Skip to content

Commit e0c81ad

Browse files
committed
Expose a shutdown API to support clean shutdown on Windows
Windows can't send signals like SIGINT/SIGTERM to arbitrary processes in a way that can support clean shutdown. With Docker, we have quite a bit of cleanup to do, and clean shutdown becomes more important, so it's useful to expose an API that the desktop app can use to trigger this directly.
1 parent 661fc18 commit e0c81ad

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/api-server.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { buildInterceptors, Interceptor, ActivationError } from './interceptors'
1717
import { ALLOWED_ORIGINS } from './constants';
1818
import { delay } from './util/promise';
1919
import { getDnsServer } from './dns-server';
20+
import { shutdown } from './shutdown';
2021

2122
const ENABLE_PLAYGROUND = false;
2223

@@ -61,6 +62,7 @@ const typeDefs = `
6162
proxyPort: Int!
6263
): Boolean!
6364
triggerUpdate: Void
65+
shutdown: Void
6466
}
6567
6668
type InterceptionConfig {
@@ -177,6 +179,13 @@ const buildResolvers = (
177179
},
178180
triggerUpdate: () => {
179181
eventEmitter.emit('update-requested');
182+
},
183+
// On Windows, there's no clean way to send signals between processes to trigger graceful
184+
// shutdown. To handle that, we use HTTP from the desktop shell, instead of inter-process
185+
// signals. This completely shuts down the server, not just a single proxy endpoint, and
186+
// should only be called once the app is fully exiting.
187+
shutdown: () => {
188+
shutdown('API call');
180189
}
181190
},
182191

src/shutdown.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ type ShutdownHandler = () => Promise<unknown>;
55
const shutdownHandlers: Array<ShutdownHandler> = [];
66

77
export function registerShutdownHandler() {
8-
process.on('SIGTERM', shutdown);
9-
process.on('SIGINT', shutdown);
8+
process.on('SIGTERM', () => shutdown('SIGTERM'));
9+
process.on('SIGINT', () => shutdown('SIGINT'));
1010
}
1111

1212
export function addShutdownHandler(handler: ShutdownHandler) {
1313
shutdownHandlers.push(handler);
1414
}
1515

16-
async function shutdown() {
17-
console.log('Shutting down...');
16+
export async function shutdown(cause: string) {
17+
console.log(`Shutting down after ${cause}...`);
1818

1919
const shutdownPromises = Promise.all(shutdownHandlers.map(
2020
async (handler) => {

0 commit comments

Comments
 (0)