-
-
Notifications
You must be signed in to change notification settings - Fork 12
fix(cli): close undici global dispatcher on exit #1488
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
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,30 @@ | ||
| /** | ||
| * Node's global `fetch` (undici) keeps a pool of keep-alive sockets open after | ||
| * a command finishes its work. Those sockets keep the event loop referenced, | ||
| * so the process lingers instead of exiting on its own (see #1237). | ||
| * | ||
| * Destroying the global dispatcher releases the pooled sockets, letting the | ||
| * loop drain naturally. This is the root-cause complement to the force-exit | ||
| * timer, which stays armed as a last-resort backstop. | ||
| * | ||
| * `destroy()` aborts in-flight requests and returns immediately rather than | ||
| * waiting for them to settle, so it can't hang the exit path. The call runs in | ||
| * a `finally` after the command has already produced its result, so it must | ||
| * never reject — a shutdown error here would otherwise mask the command's | ||
| * outcome and skip the backstop timer. | ||
| */ | ||
| const GLOBAL_DISPATCHER = Symbol.for("undici.globalDispatcher.1"); | ||
|
|
||
| type ClosableDispatcher = { destroy?: () => Promise<void> }; | ||
|
|
||
| export async function closeGlobalDispatcher(): Promise<void> { | ||
| const dispatcher = (globalThis as Record<PropertyKey, unknown>)[ | ||
| GLOBAL_DISPATCHER | ||
| ] as ClosableDispatcher | undefined; | ||
|
|
||
| try { | ||
| await dispatcher?.destroy?.(); | ||
| } catch { | ||
| // Socket teardown errors are irrelevant once we're on the way out. | ||
| } | ||
| } |
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,50 @@ | ||
| import { afterEach, describe, expect, test } from "vitest"; | ||
| import { closeGlobalDispatcher } from "../../src/lib/close-dispatcher.js"; | ||
|
|
||
| const GLOBAL_DISPATCHER = Symbol.for("undici.globalDispatcher.1"); | ||
| const global = globalThis as Record<PropertyKey, unknown>; | ||
| const original = global[GLOBAL_DISPATCHER]; | ||
|
|
||
| afterEach(() => { | ||
| if (original === undefined) { | ||
| delete global[GLOBAL_DISPATCHER]; | ||
| } else { | ||
| global[GLOBAL_DISPATCHER] = original; | ||
| } | ||
| }); | ||
|
|
||
| describe("closeGlobalDispatcher", () => { | ||
| test("destroys the global dispatcher when one is registered", async () => { | ||
| let destroyed = false; | ||
| global[GLOBAL_DISPATCHER] = { | ||
| destroy: () => { | ||
| destroyed = true; | ||
| return Promise.resolve(); | ||
| }, | ||
| }; | ||
|
|
||
| await closeGlobalDispatcher(); | ||
|
|
||
| expect(destroyed).toBe(true); | ||
| }); | ||
|
|
||
| test("resolves without throwing when no dispatcher is registered", async () => { | ||
| delete global[GLOBAL_DISPATCHER]; | ||
|
|
||
| await expect(closeGlobalDispatcher()).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| test("resolves when the dispatcher has no destroy method", async () => { | ||
| global[GLOBAL_DISPATCHER] = {}; | ||
|
|
||
| await expect(closeGlobalDispatcher()).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| test("swallows a rejection from destroy so the exit path is never disrupted", async () => { | ||
| global[GLOBAL_DISPATCHER] = { | ||
| destroy: () => Promise.reject(new Error("socket teardown failed")), | ||
| }; | ||
|
|
||
| await expect(closeGlobalDispatcher()).resolves.toBeUndefined(); | ||
| }); | ||
| }); |
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.