Skip to content

Commit 07abcbf

Browse files
committed
stop the worker manager in the isolated realm server
1 parent 50994bf commit 07abcbf

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

packages/matrix/helpers/isolated-realm-server.ts

+29-7
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export async function startServer() {
8686
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
8787
},
8888
);
89+
realmServer.unref();
8990
if (realmServer.stdout) {
9091
realmServer.stdout.on('data', (data: Buffer) =>
9192
console.log(`realm server: ${data.toString()}`),
@@ -122,25 +123,36 @@ export async function startServer() {
122123
);
123124
}
124125

125-
return new IsolatedRealmServer(realmServer, testRealmDir);
126+
return new IsolatedRealmServer(realmServer, workerManager, testRealmDir);
126127
}
127128

128129
export class IsolatedRealmServer {
129-
private stopped: (() => void) | undefined;
130+
private realmServerStopped: (() => void) | undefined;
131+
private workerManagerStopped: (() => void) | undefined;
130132
private sqlResults: ((results: string) => void) | undefined;
131133
private sqlError: ((error: string) => void) | undefined;
132134

133135
constructor(
134136
private realmServerProcess: ReturnType<typeof spawn>,
137+
private workerManagerProcess: ReturnType<typeof spawn>,
135138
readonly realmPath: string, // useful for debugging
136139
) {
140+
workerManagerProcess.on('message', (message) => {
141+
if (message === 'stopped') {
142+
if (!this.workerManagerStopped) {
143+
console.error(`received unprompted worker manager stop`);
144+
return;
145+
}
146+
this.workerManagerStopped();
147+
}
148+
});
137149
realmServerProcess.on('message', (message) => {
138150
if (message === 'stopped') {
139-
if (!this.stopped) {
151+
if (!this.realmServerStopped) {
140152
console.error(`received unprompted server stop`);
141153
return;
142154
}
143-
this.stopped();
155+
this.realmServerStopped();
144156
} else if (
145157
typeof message === 'string' &&
146158
message.startsWith('sql-results:')
@@ -180,10 +192,20 @@ export class IsolatedRealmServer {
180192
}
181193

182194
async stop() {
183-
let stop = new Promise<void>((r) => (this.stopped = r));
195+
let realmServerStop = new Promise<void>(
196+
(r) => (this.realmServerStopped = r),
197+
);
184198
this.realmServerProcess.send('stop');
185-
await stop;
186-
this.stopped = undefined;
199+
await realmServerStop;
200+
this.realmServerStopped = undefined;
187201
this.realmServerProcess.send('kill');
202+
203+
let workerManagerStop = new Promise<void>(
204+
(r) => (this.workerManagerStopped = r),
205+
);
206+
this.workerManagerProcess.send('stop');
207+
await workerManagerStop;
208+
this.workerManagerStopped = undefined;
209+
this.workerManagerProcess.send('kill');
188210
}
189211
}

packages/realm-server/worker-manager.ts

+16
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ if (port != null) {
7777
console.error(`realm disconnected from worker manager: ${err.message}`);
7878
});
7979
});
80+
server.unref();
8081

8182
server.listen(port, () => {
8283
log.info(`worker manager listening for realm on port ${port}`);
@@ -100,6 +101,21 @@ if (port != null) {
100101
log.error(`Uncaught exception in worker manager:`, err);
101102
shutdown();
102103
});
104+
105+
process.on('message', (message) => {
106+
if (message === 'stop') {
107+
console.log(`stopping realm server on port ${port}...`);
108+
server.close(() => {
109+
console.log(`worker manager on port ${port} has stopped`);
110+
if (process.send) {
111+
process.send('stopped');
112+
}
113+
});
114+
} else if (message === 'kill') {
115+
console.log(`Ending worker manager process for ${port}...`);
116+
process.exit(0);
117+
}
118+
});
103119
}
104120

105121
(async () => {

0 commit comments

Comments
 (0)