55// across requests, and a 2026-07-28 listen stream across clients.
66
77import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
8+ import { spawn } from "node:child_process" ;
9+ import { existsSync } from "node:fs" ;
810import { connect as connectTcp } from "node:net" ;
11+ import path from "node:path" ;
12+ import { fileURLToPath , pathToFileURL } from "node:url" ;
913import {
1014 Client ,
1115 StreamableHTTPClientTransport ,
@@ -18,7 +22,12 @@ import {
1822 startHttpServer ,
1923 type RunningHttpServer ,
2024} from "../http.js" ;
21- import { parseTransport , RESOURCE_URI , run } from "../index.js" ;
25+ import {
26+ parseTransport ,
27+ releaseHeldLocks ,
28+ RESOURCE_URI ,
29+ run ,
30+ } from "../index.js" ;
2231import { call , makeTempGraph } from "./helpers.js" ;
2332
2433const alice = { name : "Alice" , entityType : "person" , observations : [ ] } ;
@@ -277,6 +286,73 @@ describe("run", () => {
277286 await expect ( fetch ( url ) ) . rejects . toThrow ( ) ;
278287 } ) ;
279288
289+ it ( "exits at once on a second signal, with that signal's status (#5595)" , async ( ) => {
290+ const running = await run ( [ "--transport=streamable-http" ] ) ;
291+ const close = running . close ;
292+ // A shutdown that never finishes, as when a request is still in flight.
293+ running . close = vi . fn ( ) . mockReturnValue ( new Promise < void > ( ( ) => { } ) ) ;
294+ added ( "SIGINT" ) [ 0 ] ( "SIGINT" ) ;
295+ expect ( process . exit ) . not . toHaveBeenCalled ( ) ;
296+ added ( "SIGTERM" ) [ 0 ] ( "SIGTERM" ) ;
297+ expect ( process . exit ) . toHaveBeenLastCalledWith ( 143 ) ;
298+ added ( "SIGINT" ) [ 0 ] ( "SIGINT" ) ;
299+ expect ( process . exit ) . toHaveBeenLastCalledWith ( 130 ) ;
300+ expect ( running . close ) . toHaveBeenCalledTimes ( 1 ) ;
301+ await close ( ) ;
302+ } ) ;
303+
304+ it ( "registers the lock cleanup for the process's exit (#5595)" , async ( ) => {
305+ const running = await run ( [ "--transport=streamable-http" ] ) ;
306+ expect ( process . listeners ( "exit" ) ) . toContain ( releaseHeldLocks ) ;
307+ await running . close ( ) ;
308+ } ) ;
309+
310+ // A real signal, sent to a real server process that is serving Streamable
311+ // HTTP and holds the lock mid-write. Windows has no SIGTERM to deliver:
312+ // process.kill() there ends the process outright, so no handler can run.
313+ it . skipIf ( process . platform === "win32" ) (
314+ "removes a lock held mid-write when SIGTERM stops the HTTP server (#5595)" ,
315+ async ( ) => {
316+ const lockPath = `${ graph . filePath } .lock` ;
317+ const distIndex = pathToFileURL (
318+ path . join (
319+ path . dirname ( fileURLToPath ( import . meta. url ) ) ,
320+ ".." ,
321+ "dist" ,
322+ "index.js" ,
323+ ) ,
324+ ) . href ;
325+ const script = `
326+ const { run, withFileLock } = await import(${ JSON . stringify ( distIndex ) } );
327+ await run(["--transport", "streamable-http"]);
328+ await withFileLock(${ JSON . stringify ( lockPath ) } , () => {
329+ console.log("held");
330+ return new Promise(() => {});
331+ });
332+ ` ;
333+ const child = spawn (
334+ process . execPath ,
335+ [ "--input-type=module" , "-e" , script ] ,
336+ { stdio : [ "ignore" , "pipe" , "ignore" ] , env : process . env } ,
337+ ) ;
338+ const exited = new Promise < number | null > ( ( resolve ) =>
339+ child . on ( "exit" , ( code ) => resolve ( code ) ) ,
340+ ) ;
341+ await new Promise < void > ( ( resolve , reject ) => {
342+ child . stdout . on ( "data" , ( data : Buffer ) => {
343+ if ( data . toString ( ) . includes ( "held" ) ) resolve ( ) ;
344+ } ) ;
345+ child . on ( "exit" , ( ) => reject ( new Error ( "exited before holding" ) ) ) ;
346+ } ) ;
347+ expect ( existsSync ( lockPath ) ) . toBe ( true ) ;
348+
349+ child . kill ( "SIGTERM" ) ;
350+
351+ expect ( await exited ) . toBe ( 0 ) ;
352+ expect ( existsSync ( lockPath ) ) . toBe ( false ) ;
353+ } ,
354+ ) ;
355+
280356 it ( "reports a shutdown that fails and exits non-zero" , async ( ) => {
281357 const running = await run ( [ "--transport=streamable-http" ] ) ;
282358 const close = running . close ;
0 commit comments