Repository navigation
Diagnostics: identify the double close behind the async Closer EBADF assertion on the asan lane #38630
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
Closed
Closed
Diagnostics: identify the double close behind the async Closer EBADF assertion on the asan lane #38630
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1475532
sys/io: report who first closed an fd when an async Closer hits EBADF…
robobun 9b718fa
[autofix.ci] apply automated fixes
autofix-ci[bot] 6f683c0
diagnostics: capture close sites with the frame-pointer walker instea…
robobun 0d89d0c
diagnostics: keep a close history per fd, record what each fd was, re…
robobun b334a7d
[autofix.ci] apply automated fixes
autofix-ci[bot] b551023
diagnostics: record epoll add/del and handle close events per fd, pri…
robobun 8ba1427
[autofix.ci] apply automated fixes
autofix-ci[bot] cc7f931
diagnostics: re-run to sample the EEXIST origin again
robobun 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| //! Debug-only (`debug_assertions`) diagnostics for double closes. | ||
| //! | ||
| //! Every successful `close(2)` issued through `bun_sys` records the calling | ||
| //! thread and a frame-pointer stack trace, keyed by fd number. When a later | ||
| //! close of the same number fails with `EBADF` (a use-after-close, or a second | ||
| //! owner closing a descriptor it does not own), the reporter prints the code | ||
| //! path that closed it first. Traces are printed as raw return addresses | ||
| //! (symbolize them against the binary with llvm-symbolizer). | ||
| #![allow(clippy::print_stderr)] | ||
|
|
||
| use std::sync::Mutex; | ||
| use std::sync::atomic::{AtomicU64, Ordering}; | ||
|
|
||
| use bun_core::{DumpStackTraceOptions, StoredTrace}; | ||
|
|
||
| #[derive(Clone, Copy)] | ||
| struct Slot { | ||
| /// 0 = never recorded. | ||
| seq: u64, | ||
| tid: i64, | ||
| trace: StoredTrace, | ||
| } | ||
|
|
||
| impl Slot { | ||
| const EMPTY: Slot = Slot { | ||
| seq: 0, | ||
| tid: 0, | ||
| trace: StoredTrace::EMPTY, | ||
| }; | ||
| } | ||
|
|
||
| const MAX_TRACKED_FD: usize = 4096; | ||
|
|
||
| static LEDGER: Mutex<[Slot; MAX_TRACKED_FD]> = Mutex::new([Slot::EMPTY; MAX_TRACKED_FD]); | ||
| static SEQ: AtomicU64 = AtomicU64::new(1); | ||
|
|
||
| pub fn current_tid() -> i64 { | ||
| // SAFETY: SYS_gettid takes no arguments and cannot fail. | ||
| unsafe { libc::syscall(libc::SYS_gettid) as i64 } | ||
| } | ||
|
|
||
| /// Record that `fd` was just closed successfully by the current thread. | ||
| pub fn record_closed(fd: i32) { | ||
| if fd < 0 || fd as usize >= MAX_TRACKED_FD { | ||
| return; | ||
| } | ||
| let slot = Slot { | ||
| seq: SEQ.fetch_add(1, Ordering::Relaxed), | ||
| tid: current_tid(), | ||
| trace: StoredTrace::capture(None), | ||
| }; | ||
| let mut ledger = match LEDGER.lock() { | ||
| Ok(l) => l, | ||
| Err(poisoned) => poisoned.into_inner(), | ||
| }; | ||
| ledger[fd as usize] = slot; | ||
| } | ||
|
|
||
| fn last_close(fd: i32) -> Option<Slot> { | ||
| if fd < 0 || fd as usize >= MAX_TRACKED_FD { | ||
| return None; | ||
| } | ||
| let ledger = match LEDGER.lock() { | ||
| Ok(l) => l, | ||
| Err(poisoned) => poisoned.into_inner(), | ||
| }; | ||
| let slot = ledger[fd as usize]; | ||
| (slot.seq != 0).then_some(slot) | ||
| } | ||
|
|
||
| /// Print an EBADF report for a close attempt of `fd` made by `what`: the | ||
| /// current stack, then the stack that closed `fd` last. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| pub fn report_ebadf(what: &str, fd: i32) { | ||
| let now = StoredTrace::capture(None); | ||
| eprintln!("\n==================== close({fd}) = EBADF ({what}) ===================="); | ||
| eprintln!("this close attempt is on tid {} at:", current_tid()); | ||
| bun_core::dump_stack_trace(&now.trace(), DumpStackTraceOptions::default()); | ||
| match last_close(fd) { | ||
| Some(slot) => { | ||
| eprintln!( | ||
| "fd {fd} was last closed successfully (close #{}) on tid {} at:", | ||
| slot.seq, slot.tid | ||
| ); | ||
| bun_core::dump_stack_trace(&slot.trace.trace(), DumpStackTraceOptions::default()); | ||
| } | ||
| None => eprintln!( | ||
| "fd {fd}: no successful close recorded through bun_sys (closed by C/C++ code, or never closed)" | ||
| ), | ||
| } | ||
| eprintln!("======================================================================\n"); | ||
| } | ||
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.
If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code