tcp_trsp: don't drop sock_mut mid-send in send()/check_connection#558
Open
hecko wants to merge 1 commit into
Open
tcp_trsp: don't drop sock_mut mid-send in send()/check_connection#558hecko wants to merge 1 commit into
hecko wants to merge 1 commit into
Conversation
send() and check_connection() queued a message under sock_mut and then armed the libevent read/write event through the *_ul helpers, which unlock sock_mut, call event_add(), and re-lock. That temporary unlock opens a race window: the tcp worker can run on_read()/on_write() (and close()) on the same socket while send() believes it still holds the lock, so send() may resume operating on a connection that has just been torn down. event_add() does not call back into our handlers synchronously and only takes libevent's own event-base lock, so it is safe to arm the event while holding sock_mut. Use the non-unlocking add_read_event()/add_write_event() variants so the socket state stays consistent for the whole critical section. No lock ordering is introduced: close() already takes sock_mut before event_del(). Backport of the fix from yeti-switch/sems (commit 596150a0, Michael Furmur). sems-server's transport code matches the pre-fix version, so the same race is present here.
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.
Bug
tcp_trsp_socket::send()andcheck_connection()push the outgoing message ontosend_qundersock_mut, then arm the libevent read/write event through theadd_write_event_ul()/add_read_event_ul()helpers. Those*_ulhelpers unlocksock_mut, callevent_add(), and re-lock:That temporary unlock opens a race window: the tcp worker can concurrently run
on_read()/on_write()— includingclose()— on the same socket whilesend()still believes it holds the lock.send()(orcheck_connection()) can then resume operating on a connection that has just been closed/torn down.Fix
Use the non-unlocking
add_read_event()/add_write_event()variants sosock_mutis held for the whole critical section. This is safe becauseevent_add()does not synchronously call back into our handlers; it only takes libevent's own event-base lock. No new lock ordering is introduced —close()already acquiressock_mutbefore callingevent_del(), so thesock_mut → evbaseorder is consistent. The obsolete comment about "unlock in add_write_event_ul" is removed. Changed translation unit compiles cleanly.Acknowledgement
Backport of the fix from yeti-switch/sems — commit
596150a0("tcp_trsp: fix deadlock because of racing between session-proc and tcp-worker") by Michael Furmur. sems-server's transport code matches the pre-fix version, so the same race is present here.Generated by Claude Code