Commit 15cb73d
Kenta Hayashi
windows: wait for shutdown teardown to finish before main returns
kardianos/service v1.2.1's windowsService.Run returns as soon as SCM STOP
is processed, which lets main return and the Go runtime call ExitProcess
before otelcol.Shutdown (running on a parallel goroutine) can complete.
In the pristine change this rarely mattered because Shutdown is intrinsically
sub-second, but a slow-shutdown plugin, a network flush, or any future
teardown work could be truncated -- the final "Shutdown complete." log
line and any pending flushes would be dropped without any indication.
Add a done-channel signalled by (*program).run and waited on by main
after s.Run() returns:
- shutdown_signal.go: runCompleteChan (chan struct{}) plus sync.Once
guard, runCompleteTimeout = 30s, signalRunComplete(), waitRunComplete().
- amazon-cloudwatch-agent.go: (*program).run defers signalRunComplete
so it fires on every code path, including the Windows fallback where
handleTerminatingSignal calls os.Exit. main's Windows service branch
waits on runCompleteChan after s.Run returns.
kardianos still reports SERVICE_STOPPED to SCM as soon as prg.Stop
returns, so the OS shutdown of other services proceeds in parallel.
Only this process is held back, and only until its own teardown finishes
-- capped at runCompleteTimeout so a stuck teardown cannot indefinitely
hold up the OS.
Tests: three new unit tests (idempotent signal, wait-then-signal,
wait-then-timeout). All 47 existing + new tests pass.
Empirically validated on Windows Server 2022: with a 10-second sleep
injected inside otelcol Service.Shutdown, the earlier change loses the
"Shutdown complete." log line entirely (main returns before Shutdown
finishes), whereas with this wait the line is preserved and the full
teardown sequence completes. No 6008 / Kernel-Power 41; total OS
shutdown +10s (matching the injected delay).1 parent a6a1a93 commit 15cb73d
3 files changed
Lines changed: 100 additions & 0 deletions
File tree
- cmd/amazon-cloudwatch-agent
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
435 | 435 | | |
436 | 436 | | |
437 | 437 | | |
| 438 | + | |
438 | 439 | | |
439 | 440 | | |
440 | 441 | | |
| |||
649 | 650 | | |
650 | 651 | | |
651 | 652 | | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
652 | 656 | | |
653 | 657 | | |
654 | 658 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
0 commit comments