-
Notifications
You must be signed in to change notification settings - Fork 9
feat: cap deal/retrievals with abort signals #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
09a51d7
fix: checks and jobs have a maximum timeout
SgtPooki a2bd22f
fix: propogate aborts down to checks
SgtPooki 963b5bd
chore: address pr comments
SgtPooki 8bde4bb
chore: address pr comments
SgtPooki c1f6963
Merge branch 'main' into 258-we-need-to-set-dealretrieval-max-timeout
SgtPooki 15735d4
Update docs/environment-variables.md
SgtPooki 1a31734
Update docs/environment-variables.md
SgtPooki d439bdc
Merge branch 'main' into 258-we-need-to-set-dealretrieval-max-timeout
SgtPooki d818327
chore: address pr comments
SgtPooki a8d668b
Merge branch 'main' into 258-we-need-to-set-dealretrieval-max-timeout
SgtPooki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { awaitWithAbort, createAbortError, delay } from "./abort-utils.js"; | ||
|
|
||
| describe("createAbortError", () => { | ||
| it("returns a generic AbortError when no signal is provided", () => { | ||
| const error = createAbortError(); | ||
| expect(error.name).toBe("AbortError"); | ||
| expect(error.message).toBe("The operation was aborted"); | ||
| }); | ||
|
|
||
| it("returns the signal reason when it is an Error", () => { | ||
| const reason = new Error("custom reason"); | ||
| const controller = new AbortController(); | ||
| controller.abort(reason); | ||
| const error = createAbortError(controller.signal); | ||
| expect(error).toBe(reason); | ||
| }); | ||
|
|
||
| it("returns an AbortError that preserves non-Error reasons", () => { | ||
| const controller = new AbortController(); | ||
| controller.abort("string reason"); | ||
| const error = createAbortError(controller.signal); | ||
| expect(error.name).toBe("AbortError"); | ||
| expect(error.message).toBe("The operation was aborted: string reason"); | ||
| expect((error as Error & { cause?: unknown }).cause).toBe("string reason"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("awaitWithAbort", () => { | ||
| it("passes through the promise when no signal is provided", async () => { | ||
| const result = await awaitWithAbort(Promise.resolve("hello")); | ||
| expect(result).toBe("hello"); | ||
| }); | ||
|
|
||
| it("throws immediately when signal is already aborted", async () => { | ||
| const controller = new AbortController(); | ||
| controller.abort(); | ||
| await expect(awaitWithAbort(Promise.resolve("hello"), controller.signal)).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it("rejects when signal aborts during pending promise", async () => { | ||
| const controller = new AbortController(); | ||
| const neverResolves = new Promise<string>(() => {}); | ||
|
|
||
| const resultPromise = awaitWithAbort(neverResolves, controller.signal); | ||
| controller.abort(new Error("test abort")); | ||
|
|
||
| await expect(resultPromise).rejects.toThrow("test abort"); | ||
| }); | ||
|
|
||
| it("resolves normally when promise resolves before abort", async () => { | ||
| const controller = new AbortController(); | ||
| const result = await awaitWithAbort(Promise.resolve(42), controller.signal); | ||
| expect(result).toBe(42); | ||
| }); | ||
|
|
||
| it("rejects with the original error when promise rejects", async () => { | ||
| const controller = new AbortController(); | ||
| const error = new Error("original error"); | ||
| await expect(awaitWithAbort(Promise.reject(error), controller.signal)).rejects.toThrow("original error"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("delay", () => { | ||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it("resolves after the specified time", async () => { | ||
| vi.useFakeTimers(); | ||
| const promise = delay(100); | ||
| vi.advanceTimersByTime(100); | ||
| await expect(promise).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it("rejects immediately when signal is already aborted", async () => { | ||
| const controller = new AbortController(); | ||
| controller.abort(); | ||
| await expect(delay(1000, controller.signal)).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it("rejects when signal aborts during delay", async () => { | ||
| const controller = new AbortController(); | ||
| const promise = delay(10_000, controller.signal); | ||
|
|
||
| controller.abort(new Error("cancelled")); | ||
| await expect(promise).rejects.toThrow("cancelled"); | ||
| }); | ||
|
|
||
| it("resolves normally when no signal is provided", async () => { | ||
| vi.useFakeTimers(); | ||
| const promise = delay(50); | ||
| vi.advanceTimersByTime(50); | ||
| await expect(promise).resolves.toBeUndefined(); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /** | ||
| * Returns the abort reason from a signal as an Error, or creates a generic AbortError. | ||
| */ | ||
| export function createAbortError(signal?: AbortSignal): Error { | ||
| const reason = signal?.reason; | ||
| if (reason instanceof Error) { | ||
| return reason; | ||
| } | ||
| const baseMessage = "The operation was aborted"; | ||
| const message = reason === undefined ? baseMessage : `${baseMessage}: ${String(reason)}`; | ||
| const error: Error & { cause?: unknown } = new Error(message); | ||
| error.name = "AbortError"; | ||
| if (reason !== undefined) { | ||
| error.cause = reason; | ||
| } | ||
| return error; | ||
| } | ||
|
|
||
| /** | ||
| * Wraps a promise so it rejects immediately when the signal fires. | ||
| * If the signal is already aborted, rejects immediately. | ||
| */ | ||
| export async function awaitWithAbort<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T> { | ||
| if (!signal) return promise; | ||
| signal.throwIfAborted(); | ||
|
|
||
| return new Promise<T>((resolve, reject) => { | ||
| const onAbort = () => reject(createAbortError(signal)); | ||
| signal.addEventListener("abort", onAbort, { once: true }); | ||
| promise.then( | ||
| (value) => { | ||
| signal.removeEventListener("abort", onAbort); | ||
| resolve(value); | ||
| }, | ||
| (error) => { | ||
| signal.removeEventListener("abort", onAbort); | ||
| reject(error); | ||
| }, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Abort-aware delay. Resolves after `ms`, or rejects immediately if signal fires. | ||
| */ | ||
| export function delay(ms: number, signal?: AbortSignal): Promise<void> { | ||
| if (!signal) return new Promise((resolve) => setTimeout(resolve, ms)); | ||
| if (signal.aborted) return Promise.reject(createAbortError(signal)); | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| const timeoutId = setTimeout(() => { | ||
| signal.removeEventListener("abort", onAbort); | ||
| resolve(); | ||
| }, ms); | ||
| const onAbort = () => { | ||
| clearTimeout(timeoutId); | ||
| reject(createAbortError(signal)); | ||
| }; | ||
| signal.addEventListener("abort", onAbort, { once: true }); | ||
| }); | ||
| } |
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.