Commit 3d4c8cd
committed
fix(rootfs/qemu-static): close 3 races flagged by codex review of 5359f27
Codex review of the previous lock-pattern commit (5359f27) found three
real holes:
1. trap_handler released the owner lock BEFORE taking the control lock.
Between fd close and flock(control) a fresh builder could enter setup,
prune+count and see live_count == 1 (only itself), then observe the
now-disabled qemu-arm and write '0' as its prior_state. Our subsequent
restore would read that overwritten state and never re-enable
qemu-arm — turning a coordinated handoff into permanent disablement.
Fix: take control.lock first, release owner UNDER control, then count
and restore. The whole transition is now atomic.
2. The post-disable arch-test failure path released the owner lock
manually and then called the trap handler. The handler's first
guard ([[ -n ${_native_armhf_owner_fd:-} ]] || return 0) saw the
already-cleared global and exited without restore — leaving qemu-arm
disabled and the state file leaking. Fix: drop control.lock and let
the handler do the release+count+restore under its own control lock,
matching the normal cleanup path.
3. Stale active window after process kill (SIGKILL / OOM / kill -9
between echo 0 and trap firing): kernel releases the fd so the
owner lock is gone, but the state file remains. The next first-owner
would observe the leftover-disabled qemu-arm and record disabled as
its new prior_state — clean shutdown then never re-enables. Fix: in
the first-owner branch, before saving prior_state, check for an
existing state file. If found and prior was '1', reconcile qemu-arm
back to enabled and remove the stale file before observing our own
prior_state. Crash recovery is now self-healing on the next build.
Same Edit additionally cleans up the disable-failure case: when
'echo 0 > qemu-arm' itself fails (no CAP_SYS_ADMIN), no state actually
changed, so leave state file and owner lock alone — the cleanup handler
will observe current==prior==1 and no-op the restore. The previous code
released owner outside control and rm-ed the state file, both of which
introduced the same race the trap_handler change closes elsewhere.
Reported-by: codex (PR #9769 lock-impl review-helper)
Assisted-by: Claude:claude-opus-4.7
Signed-off-by: Igor Velkov <325961+iav@users.noreply.github.com>1 parent 5359f27 commit 3d4c8cd
1 file changed
Lines changed: 55 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
267 | 267 | | |
268 | 268 | | |
269 | 269 | | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
274 | 295 | | |
275 | 296 | | |
276 | 297 | | |
| |||
289 | 310 | | |
290 | 311 | | |
291 | 312 | | |
292 | | - | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
293 | 319 | | |
294 | | - | |
295 | 320 | | |
296 | 321 | | |
297 | 322 | | |
| |||
311 | 336 | | |
312 | 337 | | |
313 | 338 | | |
314 | | - | |
315 | | - | |
316 | | - | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
317 | 345 | | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | 346 | | |
322 | 347 | | |
323 | 348 | | |
| |||
344 | 369 | | |
345 | 370 | | |
346 | 371 | | |
347 | | - | |
348 | | - | |
| 372 | + | |
349 | 373 | | |
350 | | - | |
351 | 374 | | |
352 | | - | |
353 | | - | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
354 | 383 | | |
355 | 384 | | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
360 | 388 | | |
| 389 | + | |
361 | 390 | | |
362 | | - | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
363 | 396 | | |
364 | 397 | | |
365 | 398 | | |
| |||
0 commit comments