Fix streaming scenes not able to be deleted (#2549)

* Don't navigate away from scene if delete failed
* Close connection on cancel
This commit is contained in:
WithoutPants 2022-05-04 09:27:22 +10:00 committed by GitHub
parent 0c2dc17e8e
commit e87fd516d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 3 deletions

View File

@ -164,7 +164,8 @@ func (rs sceneRoutes) streamTranscode(w http.ResponseWriter, r *http.Request, st
encoder := manager.GetInstance().FFMPEG
lm := manager.GetInstance().ReadLockManager
lockCtx := lm.ReadLock(r.Context(), scene.Path)
streamRequestCtx := manager.NewStreamRequestContext(w, r)
lockCtx := lm.ReadLock(streamRequestCtx, scene.Path)
defer lockCtx.Cancel()
stream, err := encoder.GetTranscodeStream(lockCtx, options)

View File

@ -1,6 +1,7 @@
package manager
import (
"context"
"net/http"
"github.com/stashapp/stash/internal/manager/config"
@ -10,6 +11,31 @@ import (
"github.com/stashapp/stash/pkg/utils"
)
type StreamRequestContext struct {
context.Context
ResponseWriter http.ResponseWriter
}
func NewStreamRequestContext(w http.ResponseWriter, r *http.Request) *StreamRequestContext {
return &StreamRequestContext{
Context: r.Context(),
ResponseWriter: w,
}
}
func (c *StreamRequestContext) Cancel() {
hj, ok := (c.ResponseWriter).(http.Hijacker)
if !ok {
return
}
// hijack and close the connection
conn, _, _ := hj.Hijack()
if conn != nil {
conn.Close()
}
}
func KillRunningStreams(scene *models.Scene, fileNamingAlgo models.HashAlgorithm) {
instance.ReadLockManager.Cancel(scene.Path)
@ -31,7 +57,8 @@ func (s *SceneServer) StreamSceneDirect(scene *models.Scene, w http.ResponseWrit
fileNamingAlgo := config.GetInstance().GetVideoFileNamingAlgorithm()
filepath := GetInstance().Paths.Scene.GetStreamPath(scene.Path, scene.GetHash(fileNamingAlgo))
lockCtx := GetInstance().ReadLockManager.ReadLock(r.Context(), filepath)
streamRequestCtx := NewStreamRequestContext(w, r)
lockCtx := GetInstance().ReadLockManager.ReadLock(streamRequestCtx, filepath)
defer lockCtx.Cancel()
http.ServeFile(w, r, filepath)
}

View File

@ -7,6 +7,10 @@ import (
"time"
)
type Cancellable interface {
Cancel()
}
type LockContext struct {
context.Context
cancel context.CancelFunc
@ -57,6 +61,16 @@ func NewReadLockManager() *ReadLockManager {
func (m *ReadLockManager) ReadLock(ctx context.Context, fn string) *LockContext {
retCtx, cancel := context.WithCancel(ctx)
// if Cancellable, call Cancel() when cancelled
cancellable, ok := ctx.(Cancellable)
if ok {
origCancel := cancel
cancel = func() {
origCancel()
cancellable.Cancel()
}
}
m.mutex.Lock()
defer m.mutex.Unlock()

View File

@ -60,11 +60,12 @@ export const DeleteScenesDialog: React.FC<IDeleteSceneDialogProps> = (
try {
await deleteScene();
Toast.success({ content: toastMessage });
props.onClose(true);
} catch (e) {
Toast.error(e);
props.onClose(false);
}
setIsDeleting(false);
props.onClose(true);
}
function funscriptPath(scenePath: string) {