Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions plugins/codex/scripts/app-server-broker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ async function main() {
let activeStreamSocket = null;
let activeStreamThreadIds = null;
const sockets = new Set();
let shutdownPromise = null;

function clearSocketOwnership(socket) {
if (activeRequestSocket === socket) {
Expand Down Expand Up @@ -99,18 +100,25 @@ async function main() {
}
}

async function shutdown(server) {
for (const socket of sockets) {
socket.end();
}
await appClient.close().catch(() => {});
await new Promise((resolve) => server.close(resolve));
if (listenTarget.kind === "unix" && fs.existsSync(listenTarget.path)) {
fs.unlinkSync(listenTarget.path);
}
if (pidFile && fs.existsSync(pidFile)) {
fs.unlinkSync(pidFile);
function shutdown(server) {
if (shutdownPromise) {
return shutdownPromise;
}
shutdownPromise = Promise.resolve().then(async () => {
const serverClosed = new Promise((resolve) => server.close(resolve));
for (const socket of sockets) {
socket.destroy();
}
await appClient.close().catch(() => {});
await serverClosed;
if (listenTarget.kind === "unix" && fs.existsSync(listenTarget.path)) {
fs.unlinkSync(listenTarget.path);
}
if (pidFile && fs.existsSync(pidFile)) {
fs.unlinkSync(pidFile);
}
});
return shutdownPromise;
}

appClient.setNotificationHandler(routeNotification);
Expand Down Expand Up @@ -233,6 +241,10 @@ async function main() {
});
});

appClient.onTerminal(() => {
void shutdown(server).finally(() => process.exit(1));
});

process.on("SIGTERM", async () => {
await shutdown(server);
process.exit(0);
Expand Down
Loading