From c162c3843db02ab6f9268fa2fd06e309e5a7e536 Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Tue, 21 Oct 2025 08:13:42 +1100 Subject: [PATCH] Add timeout to ffmpeg hardware tests (#6154) --- pkg/ffmpeg/codec_hardware.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/pkg/ffmpeg/codec_hardware.go b/pkg/ffmpeg/codec_hardware.go index 5151e7efe..4081f015d 100644 --- a/pkg/ffmpeg/codec_hardware.go +++ b/pkg/ffmpeg/codec_hardware.go @@ -5,9 +5,11 @@ import ( "context" "fmt" "math" + "os" "regexp" "strconv" "strings" + "time" "github.com/stashapp/stash/pkg/logger" "github.com/stashapp/stash/pkg/models" @@ -64,12 +66,32 @@ func (f *FFMpeg) InitHWSupport(ctx context.Context) { args = args.Format("null") args = args.Output("-") - cmd := f.Command(ctx, args) + // #6064 - add timeout to context to prevent hangs + const hwTestTimeoutSecondsDefault = 1 + hwTestTimeoutSeconds := hwTestTimeoutSecondsDefault * time.Second + + // allow timeout to be overridden with environment variable + if timeout := os.Getenv("STASH_HW_TEST_TIMEOUT"); timeout != "" { + if seconds, err := strconv.Atoi(timeout); err == nil { + hwTestTimeoutSeconds = time.Duration(seconds) * time.Second + } + } + + testCtx, cancel := context.WithTimeout(ctx, hwTestTimeoutSeconds) + defer cancel() + + cmd := f.Command(testCtx, args) + logger.Tracef("[InitHWSupport] Testing codec %s: %v", codec, cmd.Args) var stderr bytes.Buffer cmd.Stderr = &stderr if err := cmd.Run(); err != nil { + if testCtx.Err() != nil { + logger.Debugf("[InitHWSupport] Codec %s test timed out after %d seconds", codec, hwTestTimeoutSeconds) + continue + } + errOutput := stderr.String() if len(errOutput) == 0 {