Repository navigation
fix(driver): memory leaks on drop - #769
Merged
Merged
Conversation
johnnyshields
force-pushed
the
fix-asan-memleaks
branch
from
March 14, 2026 03:23
7375916 to
8456b53
Compare
Member
|
Thanks for the PR. |
Member
|
For 3, there might be ops still operating, referencing this bufing. Therefore, it's not safe to drop the buffers before releasing it. |
johnnyshields
force-pushed
the
fix-asan-memleaks
branch
from
March 15, 2026 18:19
8456b53 to
dd0bbc0
Compare
Contributor
Author
|
Reverted 2 and 3. Please give it a second look 🙇♂️ |
Berrysoft
reviewed
Mar 15, 2026
Berrysoft
approved these changes
Mar 16, 2026
Closed
Xerxes-2
added a commit
to Xerxes-2/compio
that referenced
this pull request
Aug 21, 2026
`drop_with_inflight_ops` pushed an `Asyncify` op that completes immediately, so there was never an in-flight op to leak, and the test asserted nothing: it passed with or without the `Drop for Driver` fix from compio-rs#769, even under ASan. Push a recv on an idle TCP socket so the op is genuinely in-flight, assert that `push` returns `Pending`, and detect a leaked op by checking the driver dropped the `SharedFd` clone that op owned. Verified against a mutant with `impl Drop for Driver` removed: io_uring fails, poll still passes because its registry owns the keys. The IOCP driver has no equivalent handling -- it calls `key.into_raw()` for pending overlapped ops and never reclaims them on drop -- so this test is expected to fail on Windows until that is fixed.
Xerxes-2
added a commit
to Xerxes-2/compio
that referenced
this pull request
Aug 21, 2026
`drop_with_inflight_ops` pushed an `Asyncify` op that completes immediately, so there was never an in-flight op to leak, and the test asserted nothing: it passed with or without the `Drop for Driver` fix from compio-rs#769, even under ASan. Push a recv on a bound UDP socket nobody ever sends to, so the op is genuinely in-flight, assert that `push` returns `Pending`, and detect a leaked op by checking that dropping the driver also dropped the `SharedFd` clone that op owned. Verified against a mutant with `impl Drop for Driver` removed: io_uring fails, poll still passes because its registry owns the keys. The IOCP driver has no equivalent handling -- it calls `key.into_raw()` for pending overlapped ops and never reclaims them on drop -- so this test is expected to fail on Windows until that is fixed.
Xerxes-2
added a commit
to Xerxes-2/compio
that referenced
this pull request
Aug 21, 2026
`drop_with_inflight_ops` pushed an `Asyncify` op that completes immediately, so there was never an in-flight op to leak, and the test asserted nothing: it passed with or without the `Drop for Driver` fix from compio-rs#769, even under ASan. Push a recv on a bound UDP socket nobody ever sends to, so the op is genuinely in-flight, assert that `push` returns `Pending`, and detect a leaked op by checking that dropping the driver also dropped the `SharedFd` clone that op owned. Verified against a mutant with `impl Drop for Driver` removed: io_uring fails, poll still passes because its registry owns the keys. The IOCP driver has no equivalent handling -- it calls `key.into_raw()` for pending overlapped ops and never reclaims them on drop -- so this test is expected to fail on Windows until that is fixed.
Xerxes-2
added a commit
to Xerxes-2/compio
that referenced
this pull request
Aug 21, 2026
`drop_with_inflight_ops` pushed an `Asyncify` op that completes immediately, so there was never an in-flight op to leak, and the test asserted nothing: it passed with or without the `Drop for Driver` fix from compio-rs#769, even under ASan. Push a recv on a bound UDP socket nobody ever sends to, so the op is genuinely in-flight, assert that `push` returns `Pending`, and detect a leaked op by checking that dropping the driver also dropped the `SharedFd` clone that op owned. Verified against a mutant with `impl Drop for Driver` removed: io_uring fails, poll still passes because its registry owns the keys. The IOCP driver has no equivalent handling -- it calls `key.into_raw()` for pending overlapped ops and never reclaims them on drop -- so this test is expected to fail on Windows until that is fixed.
Xerxes-2
added a commit
to Xerxes-2/compio
that referenced
this pull request
Aug 21, 2026
`drop_with_inflight_ops` pushed an `Asyncify` op that completes immediately, so there was never an in-flight op to observe, and the test asserted nothing: it passed with or without the `Drop for Driver` fix from compio-rs#769, even under ASan. Push a recv on a bound UDP socket nobody ever sends to, so the op is genuinely in-flight, assert that `push` returns `Pending`, and then check what the driver did with the op by looking at the `SharedFd` clone the op owned. The correct outcome differs per driver, so assert both sides of it. io_uring closes the ring on drop, which makes the kernel cancel the op, and the poll driver owns its keys outright, so both must release the op. IOCP is a real proactor: dropping the port does not cancel a submitted op, so the kernel may still write into its buffer, and releasing the key or the fd would be a use-after-free -- there the op must stay leaked. Verified against mutants: removing `impl Drop for Driver` fails the io_uring assertion, and taking the non-IOCP branch on Windows fails the IOCP assertion.
Xerxes-2
added a commit
to Xerxes-2/compio
that referenced
this pull request
Aug 22, 2026
`drop_with_inflight_ops` pushed an `Asyncify` op that completes immediately, so there was never an in-flight op to observe, and the test asserted nothing: it passed with or without the `Drop for Driver` fix from compio-rs#769, even under ASan. Push a recv on a bound UDP socket nobody ever sends to, so the op is genuinely in-flight, assert that `push` returns `Pending`, and then check what the driver did with the op by looking at the `SharedFd` clone the op owned. The correct outcome differs per driver, so assert both sides of it. io_uring closes the ring on drop, which makes the kernel cancel the op, and the poll driver owns its keys outright, so both must release the op. IOCP is a real proactor: dropping the port does not cancel a submitted op, so the kernel may still write into its buffer, and releasing the key or the fd would be a use-after-free -- there the op must stay leaked. Verified against mutants: removing `impl Drop for Driver` fails the io_uring assertion, and taking the non-IOCP branch on Windows fails the IOCP assertion.
Berrysoft
pushed a commit
that referenced
this pull request
Aug 22, 2026
…1018) `drop_with_inflight_ops` pushed an `Asyncify` op that completes immediately, so there was never an in-flight op to observe, and the test asserted nothing: it passed with or without the `Drop for Driver` fix from #769, even under ASan. Push a recv on a bound UDP socket nobody ever sends to, so the op is genuinely in-flight, assert that `push` returns `Pending`, and then check what the driver did with the op by looking at the `SharedFd` clone the op owned. The correct outcome differs per driver, so assert both sides of it. io_uring closes the ring on drop, which makes the kernel cancel the op, and the poll driver owns its keys outright, so both must release the op. IOCP is a real proactor: dropping the port does not cancel a submitted op, so the kernel may still write into its buffer, and releasing the key or the fd would be a use-after-free -- there the op must stay leaked. Verified against mutants: removing `impl Drop for Driver` fails the io_uring assertion, and taking the non-IOCP branch on Windows fails the IOCP assertion.
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.
I used asan to look for leaks in compio-driver and found 3 sources of memory leaks in
compio-driverwhen the driver or its resources are dropped without all operations completing. Found via AddressSanitizer + LeakSanitizer (-Zsanitizer=addresswith-Zbuild-std).Before: 12,952 bytes leaked in 18 allocations across the test suite
After: 0 bytes leaked
Changes
1. io_uring
Driver— in-flight key tracking +Dropimplpush_raw_with_keycallskey.into_raw()to leak theThinCell<RawOp>allocation as au64user_data into the io_uring submission queue. On completion,ErasedKey::from_raw()reconstructs ownership. But if theDriverdrops before all completions arrive (which happens on every normal process exit), the raw pointers are never reconstructed and the allocations leak.Fix: Track in-flight raw pointers in a
HashSet<usize>. Remove on completion. InDrop, drain the completion queue, then free any remaining tracked keys viaErasedKey::from_raw().I've also committed the asan script I used.
Edit: These have been reverted
2.FrozenKey— missingDropimpl~~
FrozenKeywrapsManuallyDrop<ErasedKey>and has noDropimpl. It's used inpush_blockingto send operations to the thread pool. If the thread pool drops the closure without running it (e.g., on shutdown), the innerErasedKeyleaks.Fix: Add
impl Drop for FrozenKeythat drops the innerErasedKey. Modifyinto_inner()to wrapselfinManuallyDropfirst, preventing double-drop on the normal (non-leak) path.~~~~ 3.
BufferPoolInner—IoUringBufRingleaks on drop~~IoUringBufRinginternally stores its mmap region and backing buffers inManuallyDropfields. It requires an explicitrelease()call (which unregisters from io_uring) to free them. If aBufferPoolis dropped without callingrelease_buffer_pool, the mmap and bufferVecs leak.Fix: Wrapbuf_ringinManuallyDropinsideBufferPoolInnerand add aDropimpl that callsIoUringBufRing::drop()(the crate's provided unsafe cleanup function that frees memory without unregistering). Updateinto_inner()to take the ring out andmem::forgetthe wrapper to prevent double-free when the caller intends to callrelease().