Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad8d9dd

Browse files
authored
feat: make it harder to skip graceful shutdown accidentally (#1327)
* feat: make it harder to skip graceful shutdown accidentally Signed-off-by: Spike Curtis <[email protected]> * fixup: don't use unbuffered signal channel Signed-off-by: Spike Curtis <[email protected]>
1 parent 0080658 commit ad8d9dd

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

cli/server.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ func server() *cobra.Command {
342342
return xerrors.Errorf("notify systemd: %w", err)
343343
}
344344

345+
// Because the graceful shutdown includes cleaning up workspaces in dev mode, we're
346+
// going to make it harder to accidentally skip the graceful shutdown by hitting ctrl+c
347+
// two or more times. So the stopChan is unlimited in size and we don't call
348+
// signal.Stop() until graceful shutdown finished--this means we swallow additional
349+
// SIGINT after the first. To get out of a graceful shutdown, the user can send SIGQUIT
350+
// with ctrl+\ or SIGTERM with `kill`.
345351
stopChan := make(chan os.Signal, 1)
346352
defer signal.Stop(stopChan)
347353
signal.Notify(stopChan, os.Interrupt)
@@ -358,12 +364,13 @@ func server() *cobra.Command {
358364
return err
359365
case <-stopChan:
360366
}
361-
signal.Stop(stopChan)
362367
_, err = daemon.SdNotify(false, daemon.SdNotifyStopping)
363368
if err != nil {
364369
return xerrors.Errorf("notify systemd: %w", err)
365370
}
366-
_, _ = fmt.Fprintln(cmd.OutOrStdout(), "\n\n"+cliui.Styles.Bold.Render("Interrupt caught. Gracefully exiting..."))
371+
_, _ = fmt.Fprintln(cmd.OutOrStdout(), "\n\n"+
372+
cliui.Styles.Bold.Render(
373+
"Interrupt caught, gracefully exiting. Use ctrl+\\ to force quit"))
367374

368375
if dev {
369376
organizations, err := client.OrganizationsByUser(cmd.Context(), codersdk.Me)

cli/server_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ func TestServer(t *testing.T) {
271271
require.NoError(t, err)
272272
err = currentProcess.Signal(os.Interrupt)
273273
require.NoError(t, err)
274+
// Send a two more signal, which should be ignored. Send 2 because the channel has a buffer
275+
// of 1 and we want to make sure that nothing strange happens if we exceed the buffer.
276+
err = currentProcess.Signal(os.Interrupt)
277+
require.NoError(t, err)
278+
err = currentProcess.Signal(os.Interrupt)
279+
require.NoError(t, err)
274280
<-done
275281
})
276282
t.Run("DatadogTracerNoLeak", func(t *testing.T) {

0 commit comments

Comments
 (0)