Skip to content

Commit

Permalink
Fix streaming scenes not able to be deleted (#2549)
Browse files Browse the repository at this point in the history
* Don't navigate away from scene if delete failed
* Close connection on cancel
  • Loading branch information
WithoutPants authored May 3, 2022
1 parent 0c2dc17 commit e87fd51
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
3 changes: 2 additions & 1 deletion internal/api/routes_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 28 additions & 1 deletion internal/manager/running_streams.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package manager

import (
"context"
"net/http"

"github.com/stashapp/stash/internal/manager/config"
Expand All @@ -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)

Expand All @@ -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)
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/fsutil/lock_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"time"
)

type Cancellable interface {
Cancel()
}

type LockContext struct {
context.Context
cancel context.CancelFunc
Expand Down Expand Up @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion ui/v2.5/src/components/Scenes/DeleteScenesDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e87fd51

Please sign in to comment.