Skip to content

fix daemonizing: don't lose an early notify signal from the background process (1.4-maint) - #10580

Merged
ThomasWaldmann merged 1 commit into
borgbackup:1.4-maintfrom
ThomasWaldmann:fix-daemonizing-early-signal-14
Nov 3, 2026
Merged

ThomasWaldmann merged 1 commit into
borgbackup:1.4-maintfrom
ThomasWaldmann:fix-daemonizing-early-signal-14

Conversation

@ThomasWaldmann

Copy link
Copy Markdown
Member

Backport of #10123 to 1.4-maint.

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 and borg mount can fail.

The fix blocks the notify signals before the fork in _daemonize() and waits for them atomically in the foreground (signal.sigwait() plus a SIGALRM timer for the timeout, as signal.sigtimedwait() does not exist on macOS). An early signal then stays pending and is reliably picked up by the wait. The background process restores the original signal mask.

Cherry-picked from db2ff30; the only conflict was the old time.sleep()-with-handlers wait block in daemonizing(), replaced by the master version (adapted to the 1.4 quote style).

🤖 Generated with Claude Code

…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>
(cherry picked from commit db2ff30)
@codecov

codecov Bot commented Oct 30, 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 82.10%. Comparing base (e81a51f) to head (275ca3c).
⚠️ Report is 12 commits behind head on 1.4-maint.

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              @@
##           1.4-maint   #10580      +/-   ##
=============================================
- Coverage      82.11%   82.10%   -0.01%     
=============================================
  Files             38       38              
  Lines          11485    11490       +5     
  Branches        1810     1814       +4     
=============================================
+ Hits            9431     9434       +3     
+ Misses          1468     1467       -1     
- Partials         586      589       +3     

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

@ThomasWaldmann
ThomasWaldmann merged commit e5de73c into borgbackup:1.4-maint Nov 3, 2026
13 of 15 checks passed
@ThomasWaldmann
ThomasWaldmann deleted the fix-daemonizing-early-signal-14 branch November 3, 2026 21:08
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