Skip to content

feat(webserver): customize shutdown with new gracefulShutdown option - #34130

Merged
Simon Knott (Skn0tt) merged 32 commits into
microsoft:mainfrom
Skn0tt:webserver-sigkill
Jan 3, 2025
Merged

Simon Knott (Skn0tt) merged 32 commits into
microsoft:mainfrom
Skn0tt:webserver-sigkill

Conversation

@Skn0tt

Copy link
Copy Markdown
Contributor

We had this change up earlier, but it staled because I was unsure what PID to signal. After doing some more research on it, it appears that both for SIGINT and SIGTERM it's appropriate to send to the entire process group. So this PR is a reopening of #33379 with those learnings.

Closes #33377, #18209

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Comment thread docs/src/test-webserver-js.md Outdated
| `stdout` | If `"pipe"`, it will pipe the stdout of the command to the process stdout. If `"ignore"`, it will ignore the stdout of the command. Default to `"ignore"`. |
| `stderr` | Whether to pipe the stderr of the command to the process stderr or ignore it. Defaults to `"pipe"`. |
| `timeout` | How long to wait for the process to start up and be available in milliseconds. Defaults to 60000. |
| `kill` | How to shut down the process gracefully. If unspecified, the process group is forcefully `SIGKILL`ed. If set to `{ SIGINT: 500 }`, the process group is sent a `SIGINT` signal, followed by `SIGKILL` if it doesn't exit within 500ms. You can also use `SIGTERM` instead. A `0` timeout means no `SIGKILL` will be sent. Windows doesn't support `SIGINT` and `SIGTERM` signals, so this option is ignored. |

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd split it into 2 fields{ signal: SIGINT', timeout 5000 } to make it clear that only one signal can be specified and what the value semantics is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also rename it from kill to shutdown or gracefulShutdown? At the moment, kill: undefined means "KILL immediately", which could be surprising to some.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored in 1361b4b

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also rename it from kill to shutdown or gracefulShutdown?

I like gracefulShutdown more. I'm sure there will be more opinions at the API review 😁

Comment thread packages/playwright/src/plugins/webServerPlugin.ts Outdated
killSet.delete(killProcessAndCleanup);
removeProcessHandlersIfNeeded();
options.log(`[pid=${spawnedProcess.pid}] <kill>`);
if (spawnedProcess.pid && !spawnedProcess.killed && !processClosed) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously we wouldn't send the signal again if it has already been received, I believe it shouldn't matter for SIGKILL handling, but would be nice to double check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was needed for when we used launchedProcess.kill(). We don't use that anymore, so i'll see if I can remove this change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, removed in 50305cc

@github-actions

This comment has been minimized.

attemptToGracefullyClose: () => Promise.reject(),
attemptToGracefullyClose: async () => {
if (process.platform === 'win32')
throw new Error('Graceful shutdown is not supported on Windows');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows users will now always see ...<gracefully close start> followed by <will force kill> in the logs. Not a big deal, but this was not the case before as we never announced graceful shutdown there. Maybe keep using kill on Windows ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think it makes any observable difference because log is a no-op anyways (see L128). So it's purely a code complexity question. If we assigned _killProcess depending on the platform, I worry that'd make us split execution more, and make this slightly harder to reason about I think. So i'll keep it as is for now.

@Skn0tt Simon Knott (Skn0tt) changed the title feat(webserver): customize shutdown with new kill option feat(webserver): customize shutdown with new gracefulShutdown option Jan 3, 2025
@github-actions

github-actions Bot commented Jan 3, 2025

Copy link
Copy Markdown
Contributor

Test results for "tests 1"

4 flaky ⚠️ [firefox-page] › page/page-evaluate.spec.ts:403:3 › should throw for too deep reference chain @firefox-ubuntu-22.04-node18
⚠️ [webkit-library] › library/proxy.spec.ts:63:3 › should work with IP:PORT notion @webkit-ubuntu-22.04-node18
⚠️ [webkit-page] › page/page-set-input-files.spec.ts:39:3 › should upload a folder @webkit-ubuntu-22.04-node18
⚠️ [playwright-test] › ui-mode-test-watch.spec.ts:145:5 › should watch all @windows-latest-node18-1

37513 passed, 653 skipped
✔️✔️✔️

Merge workflow run.

@rmunn

Copy link
Copy Markdown
Contributor

The difference between SIGINT and SIGTERM on Linux, which perhaps should be mentioned in the documentation, is that SIGINT is what is sent to the process running in the current terminal when you press Ctrl+C in that terminal. SIGTERM is the default signal sent by kill <processID> if you don't manually specify another signal, and means "please gracefully terminate".

For some programs, such as programming languages with a REPL, SIGINT is captured and treated as "return to the REPL prompt" rather than "terminate process and return to the terminal prompt". So the documentation should probably list SIGTERM as the preferred method, and give SIGINT as an alternative to SIGTERM, since SIGTERM is the signal that more closely matches the intent of gracefulShutdown.

@Skn0tt

Copy link
Copy Markdown
Contributor Author

Thanks for the context! Would you be willing to contribute those details in a docs PR? That'd be wonderful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Graceful shutdown of webservers

3 participants