-
Notifications
You must be signed in to change notification settings - Fork 0
fix: gracefulShutdown awaits async cleanup after server.close drain (v0.0.4) #7
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
53950bc
fix: gracefulShutdown awaits async cleanup after server.close drain (…
y1o1 3641ba7
fixup: address Round 1 review (closeIdleConnections + CHANGELOG + eng…
y1o1 27ca985
fixup: address Round 2 Minor (CHANGELOG wording + var rename + regres…
y1o1 8d073f9
fixup: address Round 3 Copilot (T3 idempotency guard + T4/T10 rename)
y1o1 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| # Changelog | ||
|
|
||
| All notable changes to this project will be documented in this file. | ||
|
|
||
| The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). | ||
|
|
||
| ## [0.0.4] — Unreleased | ||
|
|
||
| ### Changed | ||
|
|
||
| - **`gracefulShutdown(server, cleanup)` now awaits an async `cleanup` | ||
| callback after `server.close()` drains in-flight requests.** The | ||
| `cleanup` parameter type is widened from `() => void` to | ||
| `() => void | Promise<void>` (backward-compatible at the call site — | ||
| any function that previously satisfied `() => void` still satisfies | ||
| the new union). Cleanup errors are caught with | ||
| `console.error("gracefulShutdown: cleanup error", err)` and do not | ||
| abort `process.exit(0)`. | ||
|
|
||
| Previously, `cleanup` ran synchronously *before* `server.close()`, | ||
| which meant any returned `Promise` was dropped on the floor — the | ||
| process exited before async resource releases (Redis disconnects, | ||
| database pools, file handles) could complete. The new shape is: | ||
|
|
||
| 1. SIGTERM/SIGINT triggers the handler. | ||
| 2. `server.close()` is registered with an async callback that fires | ||
| once all sockets are closed; in parallel, | ||
| `server.closeIdleConnections()` runs synchronously to release | ||
| idle keep-alive sockets so the close callback can fire promptly. | ||
| 3. Inside the close callback, `await cleanup?.()` runs (caught + | ||
| logged on rejection), then `process.exit(0)`. | ||
|
|
||
| `server.closeIdleConnections()` deliberately leaves connections that | ||
| are mid-request alone — those drain naturally through `server.close`, | ||
| preserving the graceful-shutdown contract for in-flight requests. | ||
| (Note: starting with Node.js 19.0.0, `server.close` reaps idle | ||
| keep-alive connections on its own; the explicit | ||
| `closeIdleConnections()` call is harmless on 19+ and necessary on | ||
| 18.x.) | ||
|
|
||
| This is a behavior change. Operators relying on the old fire-and-forget | ||
| ordering — where `cleanup` started before `server.close()` and ran | ||
| concurrently with request draining — will now experience strictly | ||
| sequential, awaited cleanup after the request drain. In practice, the | ||
| new ordering is what most operators *expected* the old code to do, so | ||
| the migration impact is typically zero. | ||
|
|
||
| ### Added | ||
|
|
||
| - **`server.closeIdleConnections()` is invoked synchronously after | ||
| `server.close()` registers its callback** so the close callback can | ||
| fire promptly on Node 18.x without waiting for keep-alive timeouts. | ||
| Active in-flight requests are not affected — only idle keep-alive | ||
| connections are released. Requires Node.js >= 18.2.0; `package.json` | ||
| now declares `engines.node >= 18.19.0` which already satisfies this. | ||
| - **`engines.node >= 18.19.0`** declared in `package.json` to match the | ||
| consumer floor (`auth.provider`) and to fail fast for installers on | ||
| pre-18.2 Node where `closeIdleConnections` is undefined. | ||
| - **Idempotent under repeated signal delivery.** A `shuttingDown` guard | ||
| in the handler returns early on the second SIGTERM / SIGINT, and the | ||
| signal listeners are removed on first invocation. Operators that send | ||
| multiple SIGTERMs (k8s sending repeated TERM before falling back to | ||
| SIGKILL, or operators pressing Ctrl+C several times) no longer cause | ||
| duplicated `cleanup()` invocations or duplicate `process.exit(0)` | ||
| calls. | ||
|
|
||
| ### Migration | ||
|
|
||
| The signature widening from `() => void` to `() => void | Promise<void>` | ||
| is structurally backward-compatible: every previous caller's `cleanup` | ||
| shape still satisfies the new type. No code change is required at call | ||
| sites unless the caller explicitly wants to take advantage of awaited | ||
| async cleanup (e.g., switching from | ||
| `() => { redis.disconnect(); }` to | ||
| `async () => { await redis.disconnect(); }`). | ||
|
|
||
| For callers in `o3co/auth.provider` `templates/standalone/src/app.mts`: | ||
| the existing call `gracefulShutdown(server, () => handle.dispose())` was | ||
| already passing a function returning a `Promise` that the old code was | ||
| silently dropping. After this release, `handle.dispose()` is properly | ||
| awaited before `process.exit(0)` runs. | ||
|
|
||
| ## [0.0.3] — 2026-04-14 | ||
|
|
||
| - CI / publishing infrastructure: OIDC Trusted Publishing, dependabot, | ||
| pnpm store cache, idempotent release workflow, badges. | ||
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.