Repository navigation
Make epoll_ctl, kevent, six uSockets loop imports and Bun__FFI__closeFunctions unsafe to call
#47940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Make epoll_ctl, kevent, six uSockets loop imports and Bun__FFI__closeFunctions unsafe to call
#47940
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5338b87
sys: `linux::epoll_ctl` is an `unsafe fn` again, for the value it reg…
claude 64cde30
uws: six loop imports are unsafe again, and their callers say what th…
claude 5a30ebe
ffi: `Bun__FFI__closeFunctions` is an unsafe import again
claude 9f31e0b
TextDecoderStream: the two results of `TextDecoder__createForStream` …
claude 92bfa9a
alloc: `Zone::init` asserts that `malloc_create_zone` gave it a zone
claude 8258e3e
io: the comment on `FilePoll`'s `epoll_ctl` says what `deinit` really…
claude 44382b4
sys: `kevent` is an `unsafe fn`, for the `udata` it registers
claude 8ad7cf9
sys, io: the contracts of `epoll_ctl` and `kevent` cover the events a…
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟣 pre-existing, not blocking: Pre-existing: a FilePoll slot can be recycled while the outer epoll/kqueue batch still holds an event for it, so a handler that nests a tick can get a stale event dispatched to a fresh poll or a poisoned/freed slot. The SAFETY text at src/io/posix_event_loop.rs:631-632 says the slot is held until a tick returns with no fetched event left, but
process_deferred_freesruns fromon_after_event_loopafter every tick, nested ones included. Fix: free deferred FilePoll slots only when no outer dispatch is in progress (e.g. gate the after-loop thunk on the loop'stick_depthbeing 0, as loop.c:505 does for closed sockets), so the invariant the comment states holds for every caller oftick_with_timeout/tick_without_idle.A small fix can ride a push you are already making; otherwise a short reply is enough.
Why this was flagged
Trigger: an outer tick fetched a batch with events for slots A and B (packages/bun-usockets/src/eventing/epoll_kqueue.c:536, dispatched at :260). A's handler deinit()s B (src/io/posix_event_loop.rs:361-388 puts B on the deferred list with IgnoreUpdates at :1377) and deactivates the last registered poll so
num_pollsis 0, then runs user JS that waits synchronously (src/jsc/event_loop.rs:1270-1273wait_for_promise->auto_tick). The nestedus_loop_run_bun_tickreturns at epoll_kqueue.c:479-480 without overwritingnum_ready_polls, so the outer batch stays live, yet src/runtime/jsc_hooks.rs:1167 still callson_after_event_loop, which runsprocess_deferred_freesandhive.puts B. When the outer loop reaches B's entry,Bun__internal_dispatch_ready_poll(src/io/posix_event_loop.rs:1483-1484) readsflagsof a freed slot, or, if A's handler got slot B back fromget_init, delivers A's batch event to the new owner. Base branch behaves the same; the diff adds the SAFETY claim that rules it out.Verification: pre-existing.
auto_tick(src/runtime/jsc_hooks.rs:1167) calls(*vm).on_after_event_loop()unconditionally after every tick, nested or not.process_deferred_frees(:1307-1324) then callshive.put(current). The outer loop resumes with its original batch and callsBun__internal_dispatch_ready_pollfor B, which reads(*B).flagsat src/io/posix_event_loop.rs:1483-1484 from a slot already returned to the hive.