fix: close event monitor race in bsdsocket emulation#1792
Merged
midwan merged 1 commit intoBlitterStudio:masterfrom Feb 16, 2026
Merged
fix: close event monitor race in bsdsocket emulation#1792midwan merged 1 commit intoBlitterStudio:masterfrom
midwan merged 1 commit intoBlitterStudio:masterfrom
Conversation
The event monitor thread could deliver a stale signal or write stale SET_* flags to ftable after the application cleared SO_EVENTMASK to 0. This happened when select() returned for a socket just as the main thread was tearing down event monitoring — the monitor would call post_socket_event() before unregister_socket_events() took effect. Three changes close both vectors of the race: 1. post_socket_event() now verifies the socket still has an active event mask (REP_ALL bits in ftable) before posting. If the mask was already cleared, the event is silently dropped. 2. The SO_EVENTMASK=0 handler clears pending SET_* flags from ftable after unregistering, preventing GetSocketEvents() from returning stale events if the fd number is later reused. 3. host_CloseSocket() gets the same SET_* cleanup for consistency. Validated with bsdsocktest 0.2.3: 6 consecutive clean runs of the signals category (test 81 previously failed intermittently). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
midwan
approved these changes
Feb 16, 2026
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.
Summary
Closes a race condition in the SO_EVENTMASK event monitor that caused intermittent spurious signal delivery after event mask teardown.
The event monitor thread runs
select()concurrently with the Amiga emulation thread. When an application clearsSO_EVENTMASKto 0 (tearing down event monitoring), there is a window whereselect()has already returned with a ready result but the monitor hasn't yet processed it. If the monitor locks the mutex beforeunregister_socket_events()runs, it fires a stale event viapost_socket_event()— writing SET_* flags to ftable and queuing a signal viaaddtosigqueue(). The signal can then be delivered after the application has cleared and freed the signal bit, causing the next operation that allocates the same signal bit to see a spurious pending signal.Three one-line changes close both vectors of the race:
post_socket_event(): verify the socket still has an active event mask (ftable[sd] & REP_ALL) before posting. IfSO_EVENTMASKwas already cleared to 0, the event is silently dropped.SET_*flags fromftable[sd]after unregistering, preventingGetSocketEvents()from returning stale events if the fd number is later reused.host_CloseSocket(): sameSET_*cleanup afterunregister_socket_events()for consistency.Test plan
🤖 Generated with Claude Code