Skip to content

Commit 8fcc136

Browse files
committed
WIP: fixing uffd handling
Signed-off-by: Babis Chalios <babis.chalios@e2b.dev>
1 parent 014f451 commit 8fcc136

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

packages/orchestrator/pkg/sandbox/uffd/userfaultfd/userfaultfd.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,19 @@ func (u *Userfaultfd) Serve(
253253
noDataCounter.Log(ctx)
254254
eagainCounter.Log(ctx)
255255

256+
// Wait for all in-flight fault goroutines before processing REMOVE events.
257+
// A goroutine from the previous batch could still be executing setState(faulted)
258+
// at line 326 after its UFFDIO_COPY returned. If we process a REMOVE for the same
259+
// page before that goroutine finishes, the goroutine's setState(faulted) would
260+
// overwrite the removed state we just set.
261+
if len(removes) > 0 {
262+
if waitErr := u.wg.Wait(); waitErr != nil {
263+
u.logger.Error(ctx, "UFFD serve error waiting for goroutines before REMOVE", zap.Error(waitErr))
264+
265+
return fmt.Errorf("failed to handle uffd: %w", waitErr)
266+
}
267+
}
268+
256269
// First handle the UFFD_EVENT_REMOVE events
257270
for _, rm := range removes {
258271
u.pageTracker.setState(uintptr(rm.start), uintptr(rm.end), removed)

0 commit comments

Comments
 (0)