Conversation
aleksisch
force-pushed
the
fix-tsan-data-races
branch
3 times, most recently
from
September 24, 2026 13:54
6315e33 to
ec5a57d
Compare
hio_write/hio_close are documented thread-safe, but they read io->ready/closed unlocked while the loop thread rewrites neighbouring bits of the same bitfield word, and hio_write stamps last_write_hrtime from the loop's cur_hrtime. Make those fields atomic, and make loop->status atomic, stored before hloop_stop's wakeup so the woken loop sees STOP. Found with ThreadSanitizer.
aleksisch
force-pushed
the
fix-tsan-data-races
branch
from
September 30, 2026 18:16
ec5a57d to
4755bfa
Compare
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.
Fixes data races reported by ThreadSanitizer when
hio_write/hio_closeare called from a non-loop thread (both are documented as thread-safe).Changes
hio_s:ready,connected,closedare nowatomic_boolinstead of bitfield bits. They are read without a lock from other threads, while the loop thread rewrites neighbouring bits in the same word.hio_s:last_read_hrtime/last_write_hrtimeandhloop_s::cur_hrtimeare nowatomic_ullong, sincehio_writestampslast_write_hrtimefromcur_hrtimeon the caller's thread.hloop_s::statusis nowatomic_int.hloop_runstoresRUNNINGafter settingpid/tid, sohloop_stopsees a validtid.hloop_stopsetsSTOP(via newhatomic_exchange) before the wakeup. Before, the loop could wake, still seeRUNNING, and block again.base/hatomic.h: addATOMIC_EXCHANGE/hatomic_exchangefor C11, C++, GCC__sync, Win32 and a non-atomic fallback.sizeof(struct hio_s)grows from 424 to 432 on linux-x64.