From aafbba7d77b094f26cdb410267913c25071c32ba Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Tue, 22 Nov 2022 10:21:27 +1100 Subject: [PATCH] Prevent hang when deleting while streaming (#3169) --- internal/manager/running_streams.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/internal/manager/running_streams.go b/internal/manager/running_streams.go index 23b00b59e..b715c0c5c 100644 --- a/internal/manager/running_streams.go +++ b/internal/manager/running_streams.go @@ -5,6 +5,7 @@ import ( "errors" "io" "net/http" + "time" "github.com/stashapp/stash/internal/manager/config" "github.com/stashapp/stash/internal/static" @@ -46,7 +47,21 @@ func (c *StreamRequestContext) Cancel() { if err != nil { logger.Warnf("unable to write end of stream: %v", err) } - _ = bw.Flush() + + // flush the buffer, but don't wait indefinitely + timeout := make(chan struct{}, 1) + go func() { + _ = bw.Flush() + close(timeout) + }() + + const waitTime = time.Second + + select { + case <-timeout: + case <-time.After(waitTime): + logger.Warnf("unable to flush buffer - closing connection") + } } conn.Close()