Commit ec8e06a
feat(runner): serialize start-sync invocations via setup-window lock
The mtime-driven self-restart + the operator drop-in's ExecStartPre
together created a race window where two `hapi runner start-sync`
invocations could each see the other's runner as stale, kill it, and
then race for the runtime lock. The 2026-05-31 22:40 BST incident
captured the worst-case outcome: invocation A killed the live runner,
took SIGTERM ~200ms later from invocation B's setup, and exited
cleanly - leaving the machine offline because systemd's
Restart=on-failure does not recover from exit code 0.
Add a separate `runner.start.lock` held by `startRunner()` for the
duration of its setup window only (version check + stopRunner + new
runtime-lock acquisition + state-file write). Released as soon as the
state file is owned, so legitimate next-invocations only delay a few
hundred ms. Stale-cleanup after 15s in case a setup crashes between
acquire and release.
When a second start-sync arrives while the first is mid-setup:
1. It blocks on acquireRunnerStartLock for up to ~6s.
2. By release, the first invocation owns the runtime lock + state
file with the current version.
3. The blocked invocation's subsequent
isRunnerRunningCurrentlyInstalledHappyVersion() returns
"matching", so it exits cleanly via the existing
"Runner already running with matching version" path - no kill,
no race.
If the lock cannot be acquired within the window (another invocation
is genuinely stuck), bail out instead of compounding the problem.
Co-authored-by: Cursor <cursoragent@cursor.com>1 parent 7d3bb7a commit ec8e06a
3 files changed
Lines changed: 128 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| |||
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
83 | 92 | | |
84 | 93 | | |
85 | 94 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
274 | 274 | | |
275 | 275 | | |
276 | 276 | | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
98 | 98 | | |
99 | 99 | | |
100 | 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 | + | |
101 | 133 | | |
102 | 134 | | |
103 | 135 | | |
| |||
107 | 139 | | |
108 | 140 | | |
109 | 141 | | |
| 142 | + | |
110 | 143 | | |
111 | 144 | | |
112 | 145 | | |
113 | 146 | | |
114 | 147 | | |
115 | 148 | | |
116 | 149 | | |
| 150 | + | |
117 | 151 | | |
118 | 152 | | |
119 | 153 | | |
| |||
684 | 718 | | |
685 | 719 | | |
686 | 720 | | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
687 | 728 | | |
688 | 729 | | |
689 | 730 | | |
| |||
942 | 983 | | |
943 | 984 | | |
944 | 985 | | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
945 | 990 | | |
946 | 991 | | |
947 | 992 | | |
| |||
954 | 999 | | |
955 | 1000 | | |
956 | 1001 | | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
957 | 1005 | | |
958 | 1006 | | |
959 | 1007 | | |
| |||
0 commit comments