fix(run): honour AbortSignal at start and after polling#380
Open
tsushanth wants to merge 1 commit into
Open
Conversation
Closes replicate#370. `replicate.run()` accepted `options.signal` but only checked it as a poll exit condition. Two paths still silently completed: 1. A signal that was already aborted before `run()` was called fell through to `predictions.create()` and waited on the prediction as usual. 2. A signal that aborted during polling broke the poll loop, but the caller's awaited promise resolved with the empty output of the canceled prediction (or `undefined`) instead of throwing — the cancellation was invisible to the consumer. `run()` now: - Calls `signal.throwIfAborted()` before any HTTP work happens, matching the `fetch`/standard-AbortController contract. - Forwards `signal` to `predictions.create()` so the initial create fetch is itself abortable. - After the poll loop exits with an aborted signal, best-effort cancels the prediction (swallowing any cancel-side error so the abort remains the higher-priority signal) and then calls `signal.throwIfAborted()` so the awaited promise rejects with `AbortError`. The existing "Aborts the operation when abort signal is invoked" test is updated to assert the new throwing contract; two new regression cases cover the pre-aborted-signal short-circuit and the no-network guarantee. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Closes #370.
replicate.run()acceptedoptions.signalbut only checked it as a poll-exit condition. Two paths still silently completed:run()was called fell through topredictions.create()and waited on the prediction as usual.undefined) instead of throwing — the cancellation was invisible to the consumer.Fix
signal.throwIfAborted()before any HTTP work happens, matching thefetch/ standardAbortControllercontract.signaltopredictions.create()so the initial create fetch is itself abortable.signal.throwIfAborted()so the awaited promise rejects withAbortError.Tests
The existing
Aborts the operation when abort signal is invokedtest assertedoutputwasundefinedafter abort — that's the bug-as-spec, so it's updated to assert anAbortErroris thrown instead. Two new regression cases cover:run()short-circuits before any HTTP request fires (verified by not registering any nock scope — an unintended request would surface asNock: No match).startingandprocessingupdates before the throw.All 81 tests in
index.test.tspass.