daemon: fix deadlock when SSH client disconnects during remote builds#14865
Merged
Mic92 merged 1 commit intoNixOS:masterfrom Dec 26, 2025
Merged
daemon: fix deadlock when SSH client disconnects during remote builds#14865Mic92 merged 1 commit intoNixOS:masterfrom
Mic92 merged 1 commit intoNixOS:masterfrom
Conversation
When a remote SSH client disconnects during a long-running operation
like addToStore(), the nix-daemon can deadlock in a circular wait:
- Process A (SSH daemon): blocked reading from downstream store socket,
waiting for response from local daemon
- Process B (local daemon): blocked reading from upstream socket,
waiting for more NAR data from SSH daemon
The existing interrupt mechanism (ReceiveInterrupts + MonitorFdHup)
correctly detects the SSH disconnect and sets _isInterrupted, but the
daemon remains blocked in read() on the downstream store connection.
Even though SIGUSR1 causes read() to return EINTR, the circular
dependency prevents forward progress.
Fix this by adding shutdownConnections() to RemoteStore that calls
shutdown(fd, SHUT_RDWR) on all tracked connection file descriptors.
Register an interrupt callback in processConnection() that invokes
this method when the store is a RemoteStore. This causes any blocking
read() to return 0 (EOF), breaking the circular wait and allowing
both processes to exit cleanly.
The fix tracks connection FDs in a synchronized set, populated when
connections are created by the Pool factory. On interrupt, all FDs
are shut down regardless of whether they're idle or in-use.
xokdvium
approved these changes
Dec 26, 2025
Contributor
xokdvium
left a comment
There was a problem hiding this comment.
Overall makes sense to me. I've also seen clients lock-up on a blocking write to a local daemon socket in FdSink in the destructor (it does a blocking flush in the destructor). Will this also fix it?
Member
Author
Only if |
Contributor
|
I see, then that's a separate fix we should do. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a remote SSH client disconnects during a long-running operation like addToStore(), the nix-daemon can deadlock in a circular wait:
The existing interrupt mechanism (ReceiveInterrupts + MonitorFdHup) correctly detects the SSH disconnect and sets _isInterrupted, but the daemon remains blocked in read() on the downstream store connection. Even though SIGUSR1 causes read() to return EINTR, the circular dependency prevents forward progress.
Fix this by adding shutdownConnections() to RemoteStore that calls shutdown(fd, SHUT_RDWR) on all tracked connection file descriptors. Register an interrupt callback in processConnection() that invokes this method when the store is a RemoteStore. This causes any blocking read() to return 0 (EOF), breaking the circular wait and allowing both processes to exit cleanly.
The fix tracks connection FDs in a synchronized set, populated when connections are created by the Pool factory. On interrupt, all FDs are shut down regardless of whether they're idle or in-use.
I have this setup now deployed for a while in my two CI setups (one of them with higher load, that triggered this issue more reliable) and haven't had any lockup since for a couple of weeks. I wish the solution was a bit cleaner, maybe someone can think of a better way, but I couldn't get the interrupt code to actually leave the daemon protocol loop without this code.
The key to produce this deadlock seem to be to build the same store path from different
nix buildinstances in a concurrent way.Motivation
Context
Add 👍 to pull requests you find important.
The Nix maintainer team uses a GitHub project board to schedule and track reviews.