@@ -1867,7 +1867,7 @@ test('reads a service', async ({ services }) => {
18671867
18681868 /** `e2e mcp` running in `dir` over stdio, with a client that sends requests and tool calls to it. */
18691869 async function mcpServer ( dir : string ) {
1870- const server = spawn ( process . execPath , [ CLI , 'mcp' ] , { cwd : dir , env : { ...process . env , CI : '' } , stdio : [ 'pipe' , 'pipe' , 'pipe' ] } ) ;
1870+ const server = spawn ( process . execPath , [ CLI , 'mcp' , '--max-sessions' , '2' ] , { cwd : dir , env : { ...process . env , CI : '' , E2E_TELEMETRY_DISABLED : '1 ' } , stdio : [ 'pipe' , 'pipe' , 'pipe' ] } ) ;
18711871 const output = { stderr : '' } ;
18721872
18731873 server . stderr . on ( 'data' , ( chunk : Buffer ) => {
@@ -1907,6 +1907,21 @@ test('reads a service', async ({ services }) => {
19071907 return reply ;
19081908 } ;
19091909
1910+ const sendRequest = ( method : string , params : object ) => {
1911+ nextId += 1 ;
1912+
1913+ const id = nextId ;
1914+ const reply = new Promise < McpMessage > ( ( resolve ) => replies . set ( id , resolve ) ) ;
1915+
1916+ server . stdin . write ( `${ JSON . stringify ( { jsonrpc : '2.0' , id, method, params } ) } \n` ) ;
1917+
1918+ return { id, reply } ;
1919+ } ;
1920+
1921+ const notify = ( method : string , params : object ) => {
1922+ server . stdin . write ( `${ JSON . stringify ( { jsonrpc : '2.0' , method, params } ) } \n` ) ;
1923+ } ;
1924+
19101925 const callTool = async ( name : string , args : object ) : Promise < string > => {
19111926 const reply = await request ( 'tools/call' , { name, arguments : args } ) ;
19121927 const text = reply . result ?. content . map ( ( part ) => part . text ) . join ( '\n' ) ?? '' ;
@@ -1920,9 +1935,115 @@ test('reads a service', async ({ services }) => {
19201935
19211936 server . stdin . write ( `${ JSON . stringify ( { jsonrpc : '2.0' , method : 'notifications/initialized' } ) } \n` ) ;
19221937
1923- return { server, output, exited, request, callTool } ;
1938+ return { server, output, exited, request, sendRequest , notify , callTool } ;
19241939 }
19251940
1941+ /** Holds the app's shutdown until the test releases it. */
1942+ function heldAppProject ( port : number , ready : boolean ) : FixtureProject {
1943+ const project = createProject ( {
1944+ 'services.ts' : SERVICES ,
1945+ 'server.cjs' : SERVER . replace ( " appendFileSync('events.log', 'app stop\\n');\n process.exit(0);" , `
1946+ appendFileSync('events.log', 'app stopping\\n');
1947+ const timer = setInterval(() => {
1948+ if (!require('node:fs').existsSync('release-stop')) return;
1949+ clearInterval(timer);
1950+ appendFileSync('events.log', 'app stop\\n');
1951+ process.exit(0);
1952+ }, 10);` ) ,
1953+ 'e2e.config.ts' : CONFIG
1954+ . replace ( "url: 'http://127.0.0.1:0'," , `url: 'http://127.0.0.1:${ port } ',${ ready ? '' : " readyUrl: 'http://127.0.0.1:1/'," } ` )
1955+ . replace ( "PORT: '{port}'" , `PORT: '${ port } '` )
1956+ . replace ( 'shutdownTimeout: 2000' , 'startupTimeout: 60_000, shutdownTimeout: 15_000' )
1957+ . replace ( ' ],' , " { name: 'independent', platform: 'fake', engine: createFakeEngine().engine },\n ]," ) ,
1958+ } ) ;
1959+ projects . push ( project ) ;
1960+ return project ;
1961+ }
1962+
1963+ it . skipIf ( process . platform === 'win32' ) ( 'frees an unrelated session slot while another app is still stopping' , async ( ) => {
1964+ const project = heldAppProject ( await freePort ( ) , true ) ;
1965+ const { server, exited, callTool } = await mcpServer ( project . dir ) ;
1966+
1967+ try {
1968+ const slow = sessionId ( await callTool ( 'open_session' , { target : 'fake' } ) ) ;
1969+ const other = sessionId ( await callTool ( 'open_session' , { target : 'independent' } ) ) ;
1970+ const closing = callTool ( 'close_session' , { session : slow } ) ;
1971+ try {
1972+ await expect . poll ( ( ) => eventLines ( project ) , { timeout : 5_000 } ) . toContain ( 'app stopping' ) ;
1973+ await expect ( Promise . race ( [
1974+ callTool ( 'close_session' , { session : other } ) ,
1975+ sleep ( 2_000 , 'the unrelated close is still waiting' , { ref : false } ) ,
1976+ ] ) ) . resolves . toContain ( `Session ${ other } closed` ) ;
1977+ const next = sessionId ( await callTool ( 'open_session' , { target : 'independent' } ) ) ;
1978+ expect ( next ) . toBeTruthy ( ) ;
1979+ expect ( eventLines ( project ) ) . not . toContain ( 'app stop' ) ;
1980+ expect ( eventLines ( project ) ) . not . toContain ( 'stop alpha' ) ;
1981+ } finally {
1982+ writeFileSync ( path . join ( project . dir , 'release-stop' ) , '' ) ;
1983+ await closing ;
1984+ }
1985+ } finally {
1986+ server . stdin . end ( ) ;
1987+ expect ( await exited ) . toBe ( 0 ) ;
1988+ }
1989+ } ) ;
1990+
1991+ it . skipIf ( process . platform === 'win32' ) ( 'stops a cancelled app start before releasing its services on server shutdown' , async ( ) => {
1992+ const port = await freePort ( ) ;
1993+ const project = heldAppProject ( port , false ) ;
1994+ const { server, exited, request } = await mcpServer ( project . dir ) ;
1995+
1996+ try {
1997+ void request ( 'tools/call' , { name : 'open_session' , arguments : { target : 'fake' } } ) ;
1998+ await expect . poll ( ( ) => fetch ( `http://127.0.0.1:${ port } ` ) . then ( ( response ) => response . ok , ( ) => false ) , { timeout : 5_000 } ) . toBe ( true ) ;
1999+ server . stdin . end ( ) ;
2000+ await expect . poll ( ( ) => eventLines ( project ) , { timeout : 5_000 } ) . toContain ( 'app stopping' ) ;
2001+ } finally {
2002+ writeFileSync ( path . join ( project . dir , 'release-stop' ) , '' ) ;
2003+ server . stdin . end ( ) ;
2004+ expect ( await exited ) . toBe ( 0 ) ;
2005+ }
2006+
2007+ expect ( eventLines ( project ) ) . toEqual ( [ 'start alpha' , 'start beta' , 'env' , `app start ${ port } http://alpha.test` , 'app stopping' , 'app stop' , 'stop beta' , 'stop alpha' ] ) ;
2008+ } ) ;
2009+
2010+ it . skipIf ( process . platform === 'win32' ) ( 'frees the slot when a queued reopen of a stopping target is cancelled' , async ( ) => {
2011+ const port = await freePort ( ) ;
2012+ const project = heldAppProject ( port , true ) ;
2013+ const { server, exited, sendRequest, notify, callTool } = await mcpServer ( project . dir ) ;
2014+
2015+ try {
2016+ const slow = sessionId ( await callTool ( 'open_session' , { target : 'fake' } ) ) ;
2017+ const closing = callTool ( 'close_session' , { session : slow } ) ;
2018+ try {
2019+ await expect . poll ( ( ) => eventLines ( project ) , { timeout : 5_000 } ) . toContain ( 'app stopping' ) ;
2020+ const queued = sendRequest ( 'tools/call' , { name : 'open_session' , arguments : { target : 'fake' } } ) ;
2021+ await expect . poll ( ( ) => eventLines ( project ) . filter ( ( line ) => line === 'env' ) . length , { timeout : 5_000 } ) . toBe ( 2 ) ;
2022+ await sleep ( 100 ) ;
2023+ notify ( 'notifications/cancelled' , { requestId : queued . id , reason : 'give up waiting on slow stop' } ) ;
2024+ let other = '' ;
2025+ await expect . poll ( async ( ) => {
2026+ const reply = await sendRequest ( 'tools/call' , { name : 'open_session' , arguments : { target : 'independent' } } ) . reply ;
2027+ if ( reply . result ?. isError === true ) return false ;
2028+ const text = reply . result ?. content . map ( ( part ) => part . text ) . join ( '\n' ) ?? '' ;
2029+ other = sessionId ( text ) ;
2030+ return other . length > 0 ;
2031+ } , { timeout : 2_000 } ) . toBe ( true ) ;
2032+ expect ( eventLines ( project ) ) . not . toContain ( 'app stop' ) ;
2033+ expect ( eventLines ( project ) ) . not . toContain ( 'stop alpha' ) ;
2034+ await callTool ( 'close_session' , { session : other } ) ;
2035+ } finally {
2036+ writeFileSync ( path . join ( project . dir , 'release-stop' ) , '' ) ;
2037+ await closing ;
2038+ }
2039+ } finally {
2040+ server . stdin . end ( ) ;
2041+ expect ( await exited ) . toBe ( 0 ) ;
2042+ }
2043+
2044+ expect ( eventLines ( project ) ) . toEqual ( [ 'start alpha' , 'start beta' , 'env' , `app start ${ port } http://alpha.test` , 'app stopping' , 'env' , 'app stop' , 'stop beta' , 'stop alpha' ] ) ;
2045+ } ) ;
2046+
19262047 it ( 'stops the app and then the services when the client closes stdin with sessions open, and leaves no process behind' , async ( ) => {
19272048 const port = await freePort ( ) ;
19282049
0 commit comments