-
Notifications
You must be signed in to change notification settings - Fork 29.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
watch: preserve output when gracefully restarted #54323
watch: preserve output when gracefully restarted #54323
Conversation
I tried investigating https://github.com/nodejs/node/blob/main/test/sequential/test-watch-mode.mjs to add a test case, but not sure how to proceed, help would be appreciated, thanks! Might need to add an option to the node/test/sequential/test-watch-mode.mjs Lines 118 to 120 in 56ff307
Is this condition needed to avoid flaky tests? The condition could look something like: if (
shouldIncludeGracefulRestartLog ||
(!data.startsWith('Waiting for graceful termination') && !data.startsWith('Gracefully restarted'))
) {
stdout.push(data);
} The test might look something like: it('should preserve output when --watch-preserve-output flag is passed and process is gracefully restarting', async () => {
const file = createTmpFile("console.log('running'); setTimeout(() => {}, 600);");
const args = ['--watch-preserve-output', file];
const { stderr, stdout, } = await runWriteSucceed({ file, watchedFile: file, args, shouldIncludeGracefulRestartLog: true });
assert.strictEqual(stderr, '');
assert.deepStrictEqual(stdout, [
'running',
`Completed running ${inspect(file)}`,
`Restarting ${inspect(file)}`,
`Waiting for graceful termination of ${inspect(file)}`,
`Gracefully restarted ${inspect(file)}`,
'running',
`Completed running ${inspect(file)}`,
]);
}); But I don't know how to "simulate" a slow (500ms) graceful exit, this is not working: |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #54323 +/- ##
==========================================
- Coverage 87.09% 87.09% -0.01%
==========================================
Files 647 647
Lines 181843 181843
Branches 34894 34887 -7
==========================================
- Hits 158383 158371 -12
- Misses 16766 16771 +5
- Partials 6694 6701 +7 |
do you have a minimal repro example to demonstrate that the log will be cleared? I was not able to reproduce it. |
cc @MoLow |
Ok, thanks for providing more information. Now I have a small repro here: import http from "node:http";
const host = "localhost";
const port = 8000;
const server = http.createServer();
server.listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`);
setTimeout(() => {
process.kill(process.pid);
console.log("killing the process");
}, 1000);
});
process.on("SIGTERM", () => {
console.log("Received SIGTERM, shutting down gracefully...");
setTimeout(() => {
process.exit(0);
}, 10000);
}); When you see |
Thank you for taking the time to make a small reproduction example, I tried, and indeed the bug shows in this small example. Might use this small repro to make a test. |
I am on vacation without a computer until mid-september, I will give a look when I am back if no one else does before me |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation LGTM, please add a test similar to the one added in #45717
@MoLow Thank you for coming back to this PR. When I opened this PR, I already tried to add a test in Could you please explain a little bit more, the reasoning behind this condition: The code being edited by this PR is not already covered by tests on I still tried again, and I've done that: it('should preserve output when --watch-preserve-output flag is passed and process is gracefully restarting', async () => {
const file = createTmpFile(`
import http from "node:http";
const host = "0.0.0.0";
const port = 8_000;
const server = http.createServer();
server.listen(port, host, () => {
console.log(\`Server is running on http://\${host}:\${port}\`);
setTimeout(() => {
process.kill(process.pid);
console.log("killing the process");
}, 1_000);
});
process.on("SIGTERM", () => {
console.log("Received SIGTERM, shutting down gracefully...");
setTimeout(() => {
process.exit(0);
}, 5_000);
});
`);
const args = ['--watch-preserve-output', file];
const { stderr, stdout, } = await runWriteSucceed({ file, watchedFile: file, args, shouldIncludeGracefulRestartLog: true });
assert.strictEqual(stderr, '');
assert.deepStrictEqual(stdout, [
'running',
`Completed running ${inspect(file)}`,
`Restarting ${inspect(file)}`,
`Waiting for graceful termination of ${inspect(file)}`,
`Gracefully restarted ${inspect(file)}`,
'running',
`Completed running ${inspect(file)}`,
]);
});
}); I've basically took the minimal reproduction example from @jakecastelli, and tried making a test with it. But executing |
These are unrelated test failures, right? |
Yes, looks unrelated, I have kicked started a rerun |
Landed in 89a2f56 |
#45717 added
--watch-preserve-output
:However, when the process is "gracefully restarting" (if the process takes more than 500ms to stop), it is still clearing the console which is unexpected with
--watch-preserve-output
.This PR fixes this bug.