File tree 2 files changed +39
-0
lines changed
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { Server } from "../server/index.js" ;
2
+ import { StdioServerTransport } from "../server/stdio.js" ;
3
+
4
+ describe ( "Process cleanup" , ( ) => {
5
+ jest . setTimeout ( 5000 ) ; // 5 second timeout
6
+
7
+ it ( "should exit cleanly after closing transport" , async ( ) => {
8
+ const server = new Server (
9
+ {
10
+ name : "test-server" ,
11
+ version : "1.0.0" ,
12
+ } ,
13
+ {
14
+ capabilities : { } ,
15
+ }
16
+ ) ;
17
+
18
+ const transport = new StdioServerTransport ( ) ;
19
+ await server . connect ( transport ) ;
20
+
21
+ // Close the transport
22
+ await transport . close ( ) ;
23
+
24
+ // If we reach here without hanging, the test passes
25
+ // The test runner will fail if the process hangs
26
+ expect ( true ) . toBe ( true ) ;
27
+ } ) ;
28
+ } ) ;
Original file line number Diff line number Diff line change @@ -62,8 +62,19 @@ export class StdioServerTransport implements Transport {
62
62
}
63
63
64
64
async close ( ) : Promise < void > {
65
+ // Remove our event listeners first
65
66
this . _stdin . off ( "data" , this . _ondata ) ;
66
67
this . _stdin . off ( "error" , this . _onerror ) ;
68
+
69
+ // Check if we were the only data listener
70
+ const remainingDataListeners = this . _stdin . listenerCount ( 'data' ) ;
71
+ if ( remainingDataListeners === 0 ) {
72
+ // Only pause stdin if we were the only listener
73
+ // This prevents interfering with other parts of the application that might be using stdin
74
+ this . _stdin . pause ( ) ;
75
+ }
76
+
77
+ // Clear the buffer and notify closure
67
78
this . _readBuffer . clear ( ) ;
68
79
this . onclose ?.( ) ;
69
80
}
You can’t perform that action at this time.
0 commit comments