File tree Expand file tree Collapse file tree 2 files changed +28
-18
lines changed Expand file tree Collapse file tree 2 files changed +28
-18
lines changed Original file line number Diff line number Diff line change
1
+ - fix(api-server): prevent race condition in server restart process (#11731 ) by @o0charlie0o
Original file line number Diff line number Diff line change @@ -94,24 +94,33 @@ export class ServerManager {
94
94
95
95
// Try to gracefully close the server
96
96
// If it doesn't close within 2 seconds, forcefully close it
97
- await Promise . race ( [
98
- new Promise < void > ( ( resolve ) => {
99
- console . log ( chalk . yellow ( 'Shutting down API server.' ) )
100
- this . httpServerProcess ! . on ( 'exit' , ( ) => resolve ( ) )
101
- this . httpServerProcess ! . kill ( )
102
- } ) ,
103
- new Promise < void > ( ( resolve ) =>
104
- setTimeout ( ( ) => {
105
- console . log (
106
- chalk . yellow (
107
- 'API server did not exit within 2 seconds, forcefully closing it.' ,
108
- ) ,
109
- )
110
- this . httpServerProcess ! . kill ( 'SIGKILL' )
111
- resolve ( )
112
- } , 2000 ) ,
113
- ) ,
114
- ] )
97
+ await new Promise < void > ( ( resolve ) => {
98
+ console . log ( chalk . yellow ( 'Shutting down API server.' ) )
99
+
100
+ const cleanup = ( ) => {
101
+ this . httpServerProcess ?. removeAllListeners ( 'exit' )
102
+ clearTimeout ( forceKillTimeout )
103
+ }
104
+
105
+ this . httpServerProcess ?. on ( 'exit' , ( ) => {
106
+ console . log ( chalk . yellow ( 'API server exited.' ) )
107
+ cleanup ( )
108
+ resolve ( )
109
+ } )
110
+
111
+ const forceKillTimeout = setTimeout ( ( ) => {
112
+ console . log (
113
+ chalk . yellow (
114
+ 'API server did not exit within 2 seconds, forcefully closing it.' ,
115
+ ) ,
116
+ )
117
+ cleanup ( )
118
+ this . httpServerProcess ?. kill ( 'SIGKILL' )
119
+ resolve ( )
120
+ } , 2000 )
121
+
122
+ this . httpServerProcess ?. kill ( )
123
+ } )
115
124
}
116
125
}
117
126
You can’t perform that action at this time.
0 commit comments