Repository navigation
fix: report a poll still in flight at the deadline as a timeout #869
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
4 commits
Select commit
Hold shift + click to select a range
e17b14c
fix: report a poll still in flight at the deadline as a timeout
zereight 922df73
fix: keep errors the deadline signal did not cause
zereight bb556fa
fix: treat a body read aborted by the deadline as a timeout
zereight f6905f2
test: check that the body-read case is the one that timed out
zereight 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
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,118 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { createServer } from "node:http"; | ||
| import type { AddressInfo } from "node:net"; | ||
| import { describe, test } from "node:test"; | ||
| import { fetchStatusBeforeDeadline, POLL_TIMEOUT_MESSAGE } from "../../utils/poll-deadline.js"; | ||
|
|
||
| // AbortSignal.timeout does not keep the process alive, so hold it open until the abort. | ||
| function waitForAbort(signal: AbortSignal): Promise<void> { | ||
| return new Promise(resolve => { | ||
| if (signal.aborted) { | ||
| resolve(); | ||
| return; | ||
| } | ||
| const keepAlive = setInterval(() => {}, 1000); | ||
| signal.addEventListener( | ||
| "abort", | ||
| () => { | ||
| clearInterval(keepAlive); | ||
| resolve(); | ||
| }, | ||
| { once: true } | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| async function rejectAfterAbort(signal: AbortSignal, error: () => unknown): Promise<never> { | ||
| await waitForAbort(signal); | ||
| throw error(); | ||
| } | ||
|
|
||
| describe("When fetchStatusBeforeDeadline runs", () => { | ||
| test("should return the fetched value before the deadline", async () => { | ||
| const value = await fetchStatusBeforeDeadline(async () => "finished", Date.now() + 1000); | ||
|
|
||
| assert.equal(value, "finished"); | ||
| }); | ||
|
|
||
| test("should time out without fetching once the deadline has passed", async () => { | ||
| let fetched = false; | ||
|
|
||
| await assert.rejects( | ||
| fetchStatusBeforeDeadline(async () => { | ||
| fetched = true; | ||
| return "finished"; | ||
| }, Date.now() - 1), | ||
| { message: POLL_TIMEOUT_MESSAGE } | ||
| ); | ||
| assert.equal(fetched, false); | ||
| }); | ||
|
|
||
| test("should report an abort by the deadline signal as a timeout", async () => { | ||
| let reason: unknown; | ||
|
|
||
| await assert.rejects( | ||
| fetchStatusBeforeDeadline( | ||
| signal => | ||
| rejectAfterAbort(signal, () => { | ||
| reason = signal.reason; | ||
| return signal.reason; | ||
| }), | ||
| Date.now() + 20 | ||
| ), | ||
| (error: Error) => { | ||
| assert.equal(error.message, POLL_TIMEOUT_MESSAGE); | ||
| assert.equal(error.cause, reason); | ||
| return true; | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| test("should report a body read that starts after the deadline as a timeout", async () => { | ||
| const server = createServer((_req, res) => { | ||
| res.writeHead(200, { "content-type": "application/json" }); | ||
| res.end('{"status":"running"}'); | ||
| }); | ||
| await new Promise<void>(resolve => server.listen(0, "127.0.0.1", resolve)); | ||
| const { port } = server.address() as AddressInfo; | ||
|
|
||
| try { | ||
| await assert.rejects( | ||
| fetchStatusBeforeDeadline(async signal => { | ||
| const response = await fetch(`http://127.0.0.1:${port}/`, { signal }); | ||
| await waitForAbort(signal); | ||
| return response.json(); | ||
| }, Date.now() + 1000), | ||
| (error: Error) => { | ||
| assert.equal(error.message, POLL_TIMEOUT_MESSAGE); | ||
| // An abort of the request itself would leave the TimeoutError reason here. | ||
| assert.equal((error.cause as Error).name, "AbortError"); | ||
| return true; | ||
| } | ||
| ); | ||
| } finally { | ||
| server.closeAllConnections(); | ||
| await new Promise(resolve => server.close(resolve)); | ||
| } | ||
| }); | ||
|
|
||
| test("should keep another error that lands after the deadline", async () => { | ||
| const failure = new Error("GitLab API error: 500 Internal Server Error"); | ||
|
|
||
| await assert.rejects( | ||
| fetchStatusBeforeDeadline(signal => rejectAfterAbort(signal, () => failure), Date.now() + 20), | ||
| error => error === failure | ||
| ); | ||
| }); | ||
|
|
||
| test("should keep an error that lands before the deadline", async () => { | ||
| const failure = new Error("GitLab API error: 404 Not Found"); | ||
|
|
||
| await assert.rejects( | ||
| fetchStatusBeforeDeadline(async () => { | ||
| throw failure; | ||
| }, Date.now() + 1000), | ||
| error => error === failure | ||
| ); | ||
| }); | ||
| }); | ||
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,31 @@ | ||
| export const POLL_TIMEOUT_MESSAGE = "Timed out waiting for terminal status"; | ||
|
|
||
| function isAbortError(error: unknown): boolean { | ||
| return error instanceof Error && error.name === "AbortError"; | ||
| } | ||
|
|
||
| /** | ||
| * Fetch one poll result inside the deadline. When the deadline signal aborts the | ||
| * request, the wait has timed out, so report that instead of the abort. Any other | ||
| * rejection passes through unchanged, even one that lands after the deadline. | ||
| */ | ||
| export async function fetchStatusBeforeDeadline<T>( | ||
| fetchStatus: (signal: AbortSignal) => Promise<T>, | ||
| deadline: number | ||
| ): Promise<T> { | ||
| const remaining = deadline - Date.now(); | ||
| if (remaining <= 0) { | ||
| throw new Error(POLL_TIMEOUT_MESSAGE); | ||
| } | ||
| const signal = AbortSignal.timeout(remaining); | ||
| try { | ||
| return await fetchStatus(signal); | ||
| } catch (error) { | ||
| // fetch rejects with signal.reason itself when this signal aborts it. A body read | ||
| // that starts after the abort throws a new AbortError instead. | ||
| if (signal.aborted && (error === signal.reason || isAbortError(error))) { | ||
| throw new Error(POLL_TIMEOUT_MESSAGE, { cause: error }); | ||
| } | ||
| throw error; | ||
| } | ||
| } |
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.