MCPcopy
hub / github.com/garrytan/gstack / shutdown

Function shutdown

browse/src/server.ts:1582–1618  ·  view source on GitHub ↗
(exitCode: number = 0)

Source from the content-addressed store, hash-verified

1580 // embedders that pass their own BrowserManager get correct teardown.
1581 // Module-level shutdown was deleted in v1.35.0.0.
1582 async function shutdown(exitCode: number = 0) {
1583 if (isShuttingDown) return;
1584 isShuttingDown = true;
1585
1586 console.log('[browse] Shutting down...');
1587 if (ownsTerminalAgent) {
1588 // Identity-based kill (v1.44+). Replaces the v1.43- `pkill -f
1589 // terminal-agent\.ts` regex teardown which matched sibling gstack
1590 // sessions on the same host. Only the PID recorded in
1591 // `<stateDir>/terminal-agent-pid` by THIS daemon's agent is signaled.
1592 try {
1593 const stateDir = path.dirname(config.stateFile);
1594 const record = readAgentRecord(stateDir);
1595 if (record) killAgentByRecord(record, 'SIGTERM');
1596 } catch (err: any) {
1597 console.warn('[browse] Failed to kill terminal-agent:', err.message);
1598 }
1599 safeUnlinkQuiet(path.join(path.dirname(config.stateFile), 'terminal-port'));
1600 safeUnlinkQuiet(path.join(path.dirname(config.stateFile), 'terminal-internal-token'));
1601 safeUnlinkQuiet(agentRecordPath(path.dirname(config.stateFile)));
1602 }
1603 try { detachSession(); } catch (err: any) {
1604 console.warn('[browse] Failed to detach CDP session:', err.message);
1605 }
1606 inspectorSubscribers.clear();
1607 if (cfgBrowserManager.isWatching()) cfgBrowserManager.stopWatch();
1608 clearInterval(flushInterval);
1609 clearInterval(idleCheckInterval);
1610 if (agentWatchdogInterval) clearInterval(agentWatchdogInterval);
1611 await flushBuffers();
1612
1613 await cfgBrowserManager.close();
1614
1615 cleanSingletonLocks(resolveChromiumProfile());
1616 safeUnlinkQuiet(config.stateFile);
1617 process.exit(exitCode);
1618 }
1619
1620 // Named lifecycle helper (matches closeTunnel style). Logs failures so
1621 // future debugging isn't blind to a stuck listener.

Callers 1

handleMetaCommandFunction · 0.70

Calls 12

readAgentRecordFunction · 0.90
killAgentByRecordFunction · 0.90
safeUnlinkQuietFunction · 0.90
agentRecordPathFunction · 0.90
detachSessionFunction · 0.90
cleanSingletonLocksFunction · 0.90
resolveChromiumProfileFunction · 0.90
flushBuffersFunction · 0.85
isWatchingMethod · 0.80
stopWatchMethod · 0.80
closeMethod · 0.65
clearMethod · 0.45

Tested by

no test coverage detected