Commit 5ab6183
authored
Gracefully Shutdown Foreground Server on Interrupt (#2863)
The solution here avoids the competing signal handlers by:
Removing the os.Exit(0) from the default handler.
When the default handler receives a signal, cancel the context that is plumbed to the run command.
Remove the run signal handler and instead rely on context cancellation.
This solution is not without risk: Commands that don’t check for context cancellation and relied on the os.Exit(0) may now continue running until they hit code that does respect the context cancellation. Based on my understanding of the code, thv run --foreground is one of the few commands that can be long running, so I think this risk is relatively small.
Beyond the scope of this pull request, I think we should prefer to rely on context cancellation rather than using signal.Notify deep in the implementation of commands. This “competing signal handlers” problem likely exists elsewhere: I count 10 uses of signal.Notify.
---------
Signed-off-by: Jeremy Drouillard <jeremy@stacklok.com>1 parent c201f38 commit 5ab6183
2 files changed
+12
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
26 | | - | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
| 51 | + | |
| 52 | + | |
50 | 53 | | |
51 | | - | |
| 54 | + | |
52 | 55 | | |
53 | 56 | | |
54 | 57 | | |
| |||
59 | 62 | | |
60 | 63 | | |
61 | 64 | | |
62 | | - | |
| 65 | + | |
63 | 66 | | |
64 | 67 | | |
65 | 68 | | |
| 69 | + | |
66 | 70 | | |
67 | 71 | | |
68 | 72 | | |
69 | 73 | | |
70 | | - | |
| 74 | + | |
71 | 75 | | |
| 76 | + | |
| 77 | + | |
72 | 78 | | |
73 | 79 | | |
74 | 80 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | 11 | | |
13 | | - | |
14 | 12 | | |
15 | 13 | | |
16 | 14 | | |
| |||
354 | 352 | | |
355 | 353 | | |
356 | 354 | | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
361 | 355 | | |
362 | 356 | | |
363 | 357 | | |
| |||
399 | 393 | | |
400 | 394 | | |
401 | 395 | | |
402 | | - | |
403 | | - | |
| 396 | + | |
| 397 | + | |
404 | 398 | | |
405 | 399 | | |
406 | 400 | | |
| |||
0 commit comments