Repository navigation
server: fix data race on handshakeDeadline after the handshake - #14
Merged
Merged
Conversation
HandleConn cleared the handshake deadline by writing the field directly after the handshake completed. The x/crypto transport goroutines started by the handshake call Read and Write concurrently; both recompute deadlines via updateDeadline when IdleTimeout is set, which reads the same field without synchronization. The race is only reachable with IdleTimeout set, which is why it only showed up in the tailscale SSH servers when TS_SSH_IDLE_TIMEOUT was enabled. handshakeDeadline is now an atomic.Pointer[time.Time]. HandleConn stores a pointer to the deadline before the handshake starts and nil once the handshake completes; updateDeadline skips the deadline when it loads nil. All accesses are atomic loads and stores. TestServerConnHandshakeDeadlineRace fails under -race if handshakeDeadline is a non-atomic *time.Time field and passes with the atomic. TestServerHandshakeTimeoutClearedAfterHandshake checks that the handshake deadline stops applying after the handshake.
bradfitz
approved these changes
Oct 25, 2026
| if srv.HandshakeTimeout > 0 { | ||
| conn.handshakeDeadline = time.Now().Add(srv.HandshakeTimeout) | ||
| handshakeDeadline := time.Now().Add(srv.HandshakeTimeout) | ||
| conn.handshakeDeadline.Store(&handshakeDeadline) |
Member
There was a problem hiding this comment.
Or drop the local and use new(expr)
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.
HandleConn cleared the handshake deadline by writing the field directly after the handshake completed. The x/crypto transport goroutines started by the handshake call Read and Write concurrently; both recompute deadlines via updateDeadline when IdleTimeout is set, which reads the same field without synchronization. The race is only reachable with IdleTimeout set, which is why it only showed up in the tailscale SSH servers when TS_SSH_IDLE_TIMEOUT was enabled.
handshakeDeadline is now an atomic.Pointer[time.Time]. HandleConn stores a pointer to the deadline before the handshake starts and nil once the handshake completes; updateDeadline skips the deadline when it loads nil. All accesses are atomic loads and stores.
TestServerConnHandshakeDeadlineRace fails under -race if handshakeDeadline is a non-atomic *time.Time field and passes with the atomic. TestServerHandshakeTimeoutClearedAfterHandshake checks that the handshake deadline stops applying after the handshake.