fix: sporadic 'context cancelled' errors when adding downloads via HTTP API - #561
Merged
Merged
Conversation
…ext() The HTTP handler was passing r.Context() to lifecycle.Enqueue(), which gets cancelled when the HTTP connection closes or the handler returns. Since the server probe can take several seconds, the browser extension may disconnect before the probe completes, causing sporadic 'context cancelled' errors on otherwise valid downloads. Switched to currentEnqueueContext() (the application-level context that only cancels on shutdown), matching what the TUI and CLI paths already use. Added a regression test that verifies downloads succeed even when the HTTP request context is already cancelled.
Member
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Downloads added via the HTTP API (browser extension) randomly fail with:
There is no rhyme or reason — retrying the exact same download immediately succeeds.
Root Cause
enqueueDownloadRequest()incmd/root_downloads.gowas passingr.Context()(the HTTP request context) tolifecycle.Enqueue().The
Enqueuefunction performs a server probe (HEAD request to check file size, range support, filename) which can take several seconds. The HTTP request contextr.Context()gets cancelled when:This creates a race: if the probe takes longer than the HTTP connection stays alive, the context is cancelled mid-probe, aborting the enqueue.
From the debug log:
context canceledFix
Replaced
r.Context()withcurrentEnqueueContext()— the application-level context that only cancels on shutdown. This matches what the TUI path (processDownloads) and CLI path already use correctly.Testing
Added
TestHandleDownload_DoesNotUseCancelledHTTPRequestContextwhich creates an HTTP request with a pre-cancelled context and verifies the download still enqueues successfully. All existing tests pass.Greptile Summary
Fixes sporadic HTTP API enqueue failures by decoupling probe/enqueue from the HTTP request context.
enqueueDownloadRequestnow passescurrentEnqueueContext()intolifecycle.Enqueue/EnqueueWithIDinstead ofr.Context(), matching CLI/TUI paths so client disconnect or handler return no longer aborts the server probe.TestHandleDownload_DoesNotUseCancelledHTTPRequestContextcovering a pre-cancelled request context that still queues successfully.Confidence Score: 5/5
This change appears safe to merge; the context change matches existing CLI/TUI enqueue lifetime and is covered by a focused regression test.
Enqueue uses the shared application cancel-on-shutdown context with a 30s probe timeout derived from it, avoiding request-context races without introducing a new failure mode on the changed path.
What T-Rex did
Important Files Changed
Sequence Diagram
Reviews (1): Last reviewed commit: "fix: use application-level context for H..." | Re-trigger Greptile