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 encoder := manager.GetInstance().FFMPEG
lm := manager.GetInstance().ReadLockManager 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() defer lockCtx.Cancel()
stream, err := encoder.GetTranscodeStream(lockCtx, options) stream, err := encoder.GetTranscodeStream(lockCtx, options)

View file

@ -1,6 +1,7 @@
package manager package manager
import ( import (
"context"
"net/http" "net/http"
"github.com/stashapp/stash/internal/manager/config" "github.com/stashapp/stash/internal/manager/config"
@ -10,6 +11,31 @@ import (
"github.com/stashapp/stash/pkg/utils" "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) { func KillRunningStreams(scene *models.Scene, fileNamingAlgo models.HashAlgorithm) {
instance.ReadLockManager.Cancel(scene.Path) instance.ReadLockManager.Cancel(scene.Path)
@ -31,7 +57,8 @@ func (s *SceneServer) StreamSceneDirect(scene *models.Scene, w http.ResponseWrit
fileNamingAlgo := config.GetInstance().GetVideoFileNamingAlgorithm() fileNamingAlgo := config.GetInstance().GetVideoFileNamingAlgorithm()
filepath := GetInstance().Paths.Scene.GetStreamPath(scene.Path, scene.GetHash(fileNamingAlgo)) 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() defer lockCtx.Cancel()
http.ServeFile(w, r, filepath) http.ServeFile(w, r, filepath)
} }

View file

@ -7,6 +7,10 @@ import (
"time" "time"
) )
type Cancellable interface {
Cancel()
}
type LockContext struct { type LockContext struct {
context.Context context.Context
cancel context.CancelFunc cancel context.CancelFunc
@ -57,6 +61,16 @@ func NewReadLockManager() *ReadLockManager {
func (m *ReadLockManager) ReadLock(ctx context.Context, fn string) *LockContext { func (m *ReadLockManager) ReadLock(ctx context.Context, fn string) *LockContext {
retCtx, cancel := context.WithCancel(ctx) 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() m.mutex.Lock()
defer m.mutex.Unlock() defer m.mutex.Unlock()

View file

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