Repository navigation
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
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Shutdown can access an auto-freed loop, and fallback atomic types leave shared accesses insufficiently synchronized.
2 open findings
What changed in this PR
This PR addresses ThreadSanitizer-reported races in libhv’s event loop and cross-thread IO operations.
Changes:
- Converts shared status, IO flags, and timestamps to atomic types.
- Publishes thread identifiers before RUNNING and sets STOP before wakeup.
- Adds atomic exchange support across backends.
| File | Description |
|---|---|
| event/hloop.c | Reorders startup publication and shutdown signaling. |
| event/hevent.h | Changes shared fields to atomic types. |
| base/hatomic.h | Adds atomic aliases and exchange operations. |
🧠 Review effort: Balanced
Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.
| uint64_t start_hrtime; // us | ||
| uint64_t end_hrtime; | ||
| uint64_t cur_hrtime; | ||
| atomic_ullong cur_hrtime; // read by hio_write from other threads |
| if (loop == NULL) return -1; | ||
| if (loop->status == HLOOP_STATUS_STOP) return -2; | ||
| // set the status before waking the loop, or it can wake, see RUNNING and block again | ||
| if (hatomic_exchange(&loop->status, HLOOP_STATUS_STOP) == HLOOP_STATUS_STOP) return -2; |
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.