icebear0828 Claude Opus 4.6 (1M context) commited on
Commit
1301d7a
·
1 Parent(s): 2996fd8

fix: server restart hanging when active SSE connections exist

Browse files

server.close() waits for all connections to drain before calling its
callback, which hangs indefinitely with active SSE streams. Add a 3s
timeout to force shutdown, ensuring the restart handler always spawns
the new process and exits.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Files changed (1) hide show
  1. src/index.ts +11 -1
src/index.ts CHANGED
@@ -135,7 +135,7 @@ export async function startServer(options?: StartOptions): Promise<ServerHandle>
135
 
136
  const close = (): Promise<void> => {
137
  return new Promise((resolve) => {
138
- server.close(() => {
139
  stopUpdateChecker();
140
  stopProxyUpdateChecker();
141
  stopModelRefresh();
@@ -144,6 +144,16 @@ export async function startServer(options?: StartOptions): Promise<ServerHandle>
144
  cookieJar.destroy();
145
  accountPool.destroy();
146
  resolve();
 
 
 
 
 
 
 
 
 
 
147
  });
148
  });
149
  };
 
135
 
136
  const close = (): Promise<void> => {
137
  return new Promise((resolve) => {
138
+ const cleanup = () => {
139
  stopUpdateChecker();
140
  stopProxyUpdateChecker();
141
  stopModelRefresh();
 
144
  cookieJar.destroy();
145
  accountPool.destroy();
146
  resolve();
147
+ };
148
+ // Force-close after 3s if active connections prevent graceful shutdown
149
+ const forceTimer = setTimeout(() => {
150
+ console.warn("[Server] Graceful close timed out (3s), forcing shutdown...");
151
+ cleanup();
152
+ }, 3000);
153
+ forceTimer.unref();
154
+ server.close(() => {
155
+ clearTimeout(forceTimer);
156
+ cleanup();
157
  });
158
  });
159
  };