feat: memory prefetching - #1705
Merged
Merged
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f8af9d9f85
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
dobrac
marked this pull request as draft
January 9, 2026 14:14
dobrac
force-pushed
the
feat/memory-prefetching
branch
2 times, most recently
from
January 9, 2026 14:50
922bba5 to
34d1252
Compare
dobrac
force-pushed
the
feat/memory-prefetching
branch
2 times, most recently
from
January 9, 2026 15:22
45334db to
289ccff
Compare
dobrac
force-pushed
the
feat/memory-prefetching
branch
4 times, most recently
from
January 9, 2026 22:14
ed5c904 to
a075a65
Compare
dobrac
force-pushed
the
feat/memory-prefetching
branch
from
January 10, 2026 10:55
eebe756 to
84a59a6
Compare
dobrac
marked this pull request as ready for review
January 10, 2026 11:24
dobrac
force-pushed
the
feat/memory-prefetching
branch
from
January 12, 2026 11:46
696dc2c to
f342294
Compare
dobrac
force-pushed
the
feat/memory-prefetching
branch
from
January 12, 2026 11:51
f342294 to
68db802
Compare
Co-authored-by: ValentaTomas <valenta.and.thomas@gmail.com>
ValentaTomas
approved these changes
Jan 12, 2026
dobrac
force-pushed
the
feat/memory-prefetching
branch
from
January 12, 2026 15:44
cc30a81 to
be2a21c
Compare
dobrac
enabled auto-merge (squash)
January 12, 2026 15:46
kalyazin
added a commit
that referenced
this pull request
May 28, 2026
## Fix Prefault/Close race causing ENOTTY/EBADF ### Motivation Since **2026-04-26** the orchestrator has been logging `UFFD serve uffdio copy error: inappropriate ioctl for device` (ENOTTY) and `bad file descriptor` (EBADF) from the UFFD prefetch path. The error rate increased sharply on **2026-05-27** after #2522 landed. The root cause is a race between the prefetcher and sandbox teardown: ``` Prefault goroutine Close() (teardown) ──────────────────────────────── ────────────────────────────── (about to acquire RLock) syscall.Close(uffd fd) ← fd freed ← OS recycles fd number to an unrelated file acquires RLock calls UFFDIO_COPY(recycled fd) → ENOTTY (or EBADF if not yet recycled) ``` `Close()` held no lock when it freed the uffd fd. The prefetcher runs on a non-cancellable `execCtx` and has no way to observe that `Close()` has run. If the OS recycled the fd number before the prefetcher's `UFFDIO_COPY` ioctl fired, the kernel returned ENOTTY because the fd now referred to a non-uffd file. The error was benign at the sandbox level (the sandbox was already being torn down) but noisy and misleading in logs. The bug was latent from when the prefetcher was introduced in January 2026 (#1705) but only started firing after the ubuntu24 template rebuild in April populated `Prefetch.Memory` data, causing the prefetcher to actually run. ### Fix `Close()` now holds `settleRequests.Lock()` for the **entire** close sequence and is idempotent: ```go func (u *Userfaultfd) Close() error { u.settleRequests.Lock() defer u.settleRequests.Unlock() if u.closed { return nil } u.closed = true syscall.Close(u.wakeupPipe[0]) syscall.Close(u.wakeupPipe[1]) return u.fd.close() } ``` `Prefault()` checks the flag immediately after acquiring `settleRequests.RLock()`: ```go u.settleRequests.RLock() defer u.settleRequests.RUnlock() if u.closed { return nil } ``` This gives three safety guarantees: 1. Any `Prefault` caller already holding `RLock` when `Close()` runs will complete its `UFFDIO_COPY` against the still-valid fd before `Close()` can acquire the write lock. 2. Any `Prefault` call that starts after `Close()` returns will see `closed == true` and return nil without touching the fd. 3. `Close()` is idempotent: a second call returns immediately without touching already-freed fds, preventing accidental double-close of the wakeup pipe fds (and any unrelated fd the OS may have recycled those numbers to). `settleRequests` already existed for exactly this kind of serialisation (guarding the lookup→install→state-update sequence against REMOVE batches), so no new lock is introduced. In production `Serve()` drains all workers via `u.wg.Wait()` before returning, so by the time the deferred `Close()` fires the lock is always uncontended. ### Test `TestPrefaultConcurrentWithClose` deterministically reproduces the race using a `faultPhaseBeforePrefaultRLock` test hook. The goroutine is parked before it acquires `RLock`, `Close()` is called to completion, then the goroutine is released. Without the fix the test fails with `failed to fault page: failed uffdio copy: bad file descriptor`; with the fix it returns nil. --------- Signed-off-by: Nikita Kalyazin <nikita.kalyazin@e2b.dev>
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.
Note
Introduces build-time memory access mapping and runtime prefetching to reduce sandbox startup latency.
PrefetchTrackerand extends UFFD to record page-fault order/types and exposePrefetchData; addsPrefaultAPI to proactively map pagesuffd/prefetch) that fetches/copies pages in the background during resume; worker counts driven by feature flagsprefetch.memory { indices, access_types, block_size }utils.Promise; replaces async channel networking withgetNetworkSlotpromisefdexit/UFFDCloseidempotent; addsCache.InvalidateNewTrackerFromBitset)Written by Cursor Bugbot for commit be2a21c. This will update automatically on new commits. Configure here.