Skip to content

fix daemonizing: don't lose an early notify signal from the background process - #10123

Merged
ThomasWaldmann merged 1 commit into
borgbackup:masterfrom
ThomasWaldmann:fix-daemonizing-early-signal
Aug 16, 2026
Merged

fix daemonizing: don't lose an early notify signal from the background process#10123
ThomasWaldmann merged 1 commit into
borgbackup:masterfrom
ThomasWaldmann:fix-daemonizing-early-signal

Conversation

@ThomasWaldmann

Copy link
Copy Markdown
Member

The foreground process installs SIGTERM/SIGHUP/SIGINT handlers (via archiver.run) before it reaches the point where it actually waits for the background (grandchild) process to notify it via os.kill(). If the background process starts up and signals fast enough, the signal is delivered to the foreground while it is still between os.fork() and the waiting code, so the globally installed handler raises at an unexpected, uncaught place. The signal then escapes daemonizing(), bubbles up through repository teardown (NotLocked) and makes borg mount exit with rc 74.

This was observed flaky in CI with coverage's sys.monitoring backend on Python 3.14 (its first-branch lazy source parse widens the window) and the pyfuse3 backend (faster grandchild startup).

The fix blocks the notify signals before the fork in _daemonize() and waits for them atomically in the foreground. An early signal then stays pending and is reliably picked up by the wait. The background process restores the original signal mask so it keeps normal signal handling.

The wait uses signal.sigwait() plus a SIGALRM timer (signal.setitimer) rather than signal.sigtimedwait(): the latter does not exist on macOS, where it would raise AttributeError in the foreground and let it die before the background migrated the lock (breaking test_migrate_lock_alive). signal.SIGALRM in turn does not exist on Windows, where referencing it at module import time would break the import chain and the Windows PyInstaller build, so it is guarded with hasattr. Daemonizing is not supported on Windows anyway (no os.fork).

Split out of #9602, where this surfaced - the bug is independent of that PR and fixes a race that is present in master today.

@codecov

codecov Bot commented Aug 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 41.66667% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.04%. Comparing base (91b030e) to head (db2ff30).
⚠️ Report is 5 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/borg/helpers/process.py 41.66% 10 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10123      +/-   ##
==========================================
- Coverage   87.05%   87.04%   -0.01%     
==========================================
  Files         101      101              
  Lines       17848    17853       +5     
  Branches     2705     2709       +4     
==========================================
+ Hits        15537    15541       +4     
+ Misses       1609     1608       -1     
- Partials      702      704       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

…d process

The foreground process installs SIGTERM/SIGHUP/SIGINT handlers (via
archiver.run) before it reaches the point where it actually waits for the
background (grandchild) process to notify it (via os.kill). If the
background process started up and signalled fast enough, the signal was
delivered to the foreground while it was still between os.fork() and the
waiting code, so the globally installed handler raised at an unexpected,
uncaught place. The signal then escaped daemonizing(), bubbled up through
repository teardown (NotLocked) and made "borg mount" exit with rc 74.

This was observed flaky in CI with coverage's sys.monitoring backend on
Python 3.14 (its first-branch lazy source parse widens the window) and the
pyfuse3 backend (faster grandchild startup).

Fix the race by blocking the notify signals before the fork in
_daemonize() and waiting for them atomically in the foreground. An early
signal then stays pending and is reliably picked up by the wait. The
background process restores the original signal mask so it keeps normal
signal handling.

Use signal.sigwait() plus a SIGALRM timer (signal.setitimer) for the
wait, rather than signal.sigtimedwait(): the latter does not exist on
macOS, where it would raise AttributeError in the foreground and let it
die before the background migrated the lock (breaking test_migrate_lock_alive).

signal.SIGALRM in turn does not exist on Windows, where referencing it at
module import time would raise AttributeError and break the import chain
(and the Windows PyInstaller build), so guard it with hasattr, matching the
defensive getattr pattern already used for the notify signals. Daemonizing
is not supported on Windows anyway (no os.fork), so the empty SIGALRM list
has no functional effect there.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@ThomasWaldmann
ThomasWaldmann force-pushed the fix-daemonizing-early-signal branch from b3cf99e to db2ff30 Compare August 16, 2026 10:10
@ThomasWaldmann
ThomasWaldmann merged commit 114bd1e into borgbackup:master Aug 16, 2026
34 of 38 checks passed
@ThomasWaldmann
ThomasWaldmann deleted the fix-daemonizing-early-signal branch August 16, 2026 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant