Skip to content

Commit

Permalink
Merge pull request #127 from ngrok/bob/term-paper
Browse files Browse the repository at this point in the history
register with SIGTERM as well
  • Loading branch information
bobzilladev authored Mar 1, 2024
2 parents 92cd3b3 + a2358fb commit a6e2393
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
41 changes: 22 additions & 19 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 22 additions & 19 deletions trailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,28 +228,31 @@ async function ngrokLinkPipe(listener, server) {
}

// protect against multiple calls, for instance from npm
var sigintRan = false;
var sigHandlerRan = false;

function registerCleanup(listener, socket) {
process.on("SIGINT", function () {
if (sigintRan) return;
sigintRan = true;
for (const signal of ["SIGINT", "SIGTERM"]) {
process.on(signal, function () {
if (process.listenerCount(signal) > 1) {
// user has registered a handler, abort this one
return;
}

if (process.listenerCount("SIGINT") > 1) {
// user has registered a handler, abort this one
return;
}
// close listener
if (listener) {
listener.close().catch((err) => {
console.error(`Error closing listener: ${err}`);
});
}
// close webserver's socket
if (socket) socket.close();
// unregister any logging callback
loggingCallback();
});
if (sigHandlerRan) return;
sigHandlerRan = true;

// close listener
if (listener) {
listener.close().catch((err) => {
console.error(`Error closing listener: ${err}`);
});
}
// close webserver's socket
if (socket) socket.close();
// unregister any logging callback
loggingCallback();
});
}
}

function consoleLog(level) {
Expand Down

0 comments on commit a6e2393

Please sign in to comment.