GoogleCloudPlatform / functions-framework-nodejs

FaaS (Function as a service) framework for writing portable Node.js functions
Apache License 2.0
1.29k stars 160 forks source link

fix: framework fails to shutdown gracefully when exit code is a string #545

Closed garethgeorge closed 1 year ago

garethgeorge commented 1 year ago

Despite the Node.js docs indicating that the exit code should always be a number it seems this is unchecked and, in fact, process.exit may be called with a string value.

Verified experimentally:

// test.js
const process = require("process");

process.on('exit', function(code) {
   console.log(typeof code);
   console.log(code);
});

process.exit('abc');
// output
string
abc

Some Cloud Function users report errors on shutdown when either their code or a dependency calls process.exit with a string value. To account for this the exit code check is updated to use string comparison to account for the possibility of a string exit code.

conventional-commit-lint-gcf[bot] commented 1 year ago

🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use automerge label. Good luck human!

-- conventional-commit-lint bot https://conventionalcommits.org/

garethgeorge commented 1 year ago

No testing at the moment, it's unclear how we should approach testing this (beyond local exploratory testing). We don't have process lifecycle tests specific to node.js and mocha doesn't support this type of test.

I think we could possibly try to add a test that simulates emitting the event, I'm also fine with relying on exploratory tests and our conformance test coverage which should be exercising the full process lifecycle. WDYT?