-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(hooks): exit the SessionEnd hook within Claude Code's shutdown grace #992
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
Open
costajohnt
wants to merge
1
commit into
rohitg00:main
Choose a base branch
from
costajohnt:fix/991-session-end-hook-cancelled
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+79
−2
Open
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
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,77 @@ | ||
| import { describe, it, expect, beforeAll, afterAll } from "vitest"; | ||
| import { spawn } from "node:child_process"; | ||
| import { createServer, type Server } from "node:http"; | ||
| import { join } from "node:path"; | ||
| import { once } from "node:events"; | ||
|
|
||
| // Runs the BUILT hook artifact (what ships), not the TypeScript source. CI | ||
| // builds before testing; locally run `npm run build` after editing | ||
| // src/hooks/session-end.ts or this exercises stale code. | ||
| const HOOK = join( | ||
| import.meta.dirname, | ||
| "..", | ||
| "plugin", | ||
| "scripts", | ||
| "session-end.mjs", | ||
| ); | ||
|
|
||
| // A server that accepts the connection and reads the request but never sends a | ||
| // response, so the hook's fire-and-forget `fetch` stays in flight. With the | ||
| // request hanging, the only thing that ends the hook process is its deferred | ||
| // `setTimeout(() => process.exit(0), N).unref()` — which lets us measure N. | ||
| let blackHole: Server; | ||
| let blackHoleUrl: string; | ||
|
|
||
| beforeAll(async () => { | ||
| blackHole = createServer(() => { | ||
| // Intentionally never respond. | ||
| }); | ||
| blackHole.listen(0, "127.0.0.1"); | ||
| await once(blackHole, "listening"); | ||
| const address = blackHole.address(); | ||
| const port = typeof address === "object" && address ? address.port : 0; | ||
| blackHoleUrl = `http://127.0.0.1:${port}`; | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| blackHole.close(); | ||
| await once(blackHole, "close"); | ||
| }); | ||
|
|
||
| function runHook( | ||
| stdin: string, | ||
| env: Record<string, string>, | ||
| ): Promise<{ exitCode: number | null; tookMs: number }> { | ||
| return new Promise((resolve, reject) => { | ||
| const start = Date.now(); | ||
| const child = spawn(process.execPath, [HOOK], { | ||
| env: { PATH: process.env["PATH"] ?? "", ...env }, | ||
| stdio: ["pipe", "ignore", "ignore"], | ||
| }); | ||
| child.on("error", reject); | ||
| child.on("close", (exitCode) => { | ||
| resolve({ exitCode, tookMs: Date.now() - start }); | ||
| }); | ||
| child.stdin.write(stdin); | ||
| child.stdin.end(); | ||
| }); | ||
| } | ||
|
|
||
| describe("session-end hook exits within Claude Code's shutdown grace (#991)", () => { | ||
| it("exits well under the old 1500ms cap even when the memory server hangs", async () => { | ||
| const payload = JSON.stringify({ session_id: "ses_timing_test" }); | ||
| const { exitCode, tookMs } = await runHook(payload, { | ||
| AGENTMEMORY_URL: blackHoleUrl, | ||
| }); | ||
|
|
||
| expect(exitCode).toBe(0); | ||
| // Positive control: the hung request must actually hold the process open to | ||
| // the deferred-exit timer, otherwise the timing assertion is meaningless. | ||
| expect(tookMs).toBeGreaterThan(250); | ||
| // The deferred-exit timer must fire comfortably inside Claude Code's | ||
| // SessionEnd shutdown grace. The bound sits between the fixed 500ms timer | ||
| // (plus startup) and the old 1500ms cap that overran the grace, so the | ||
| // harness killed the hook and reported "Hook cancelled" (#991). | ||
| expect(tookMs).toBeLessThan(1400); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Keep the longer exit cap when
session-endfans out to multiple requests.This hook always sends
/session/end, and lines 42-64 can enqueue up to three more fire-and-forget POSTs. With those paths enabled, forcing exit after 500ms can cut the process before the later requests have flushed, sosession-endcan silently drop consolidation / bridge work. If you want the single-request path to stay under Claude Code’s grace window, make the delay conditional on whether the extra POSTs are enabled instead of shrinking everysession-endrun.Suggested change
As per the hook contract in
AGENTS.md, multi-request telemetry hooks likesession-endshould keep the 1500ms cap so all fire-and-forget requests have time to start.📝 Committable suggestion
🤖 Prompt for AI Agents