e2b-dev / e2b-dev/runtime

orchestrator: NBD pool ReleaseDevice retry sleep ignores context cancellation

Open
#3,347 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
1.6k
Forks
438
PR merge metrics
No merged PRs in 30d

Description

Problem

In packages/orchestrator/pkg/sandbox/nbd/pool.go:369, the ReleaseDevice retry loop calls time.Sleep(500 * time.Millisecond) which is not interruptible by context cancellation:

// pool.go:360-370 (simplified)
for {
    select {
    case <-ctx.Done():   // ← only checked at TOP of loop
        return ctx.Err()
    default:
    }
    attempt++
    err := d.release(ctx, idx)
    if err == nil { return nil }
    // ...
    time.Sleep(500 * time.Millisecond)  // ← BUG: ctx cancellation not checked here
}

When the orchestrator shuts down and the shutdown context is cancelled, any goroutine blocked inside this sleep will not wake up for up to 500 ms per retry. With WithInfiniteRetry() enabled (used by both DirectPathMount.Close and DevicePool.Close), a single stuck device can stall the shutdown for an unbounded number of 500 ms sleeps before the context cancel is noticed.

Affected callers

  • path_direct.go:336: ReleaseDevice(ctx, idx, WithInfiniteRetry()) — called on every sandbox stop
  • pool.go:395: ReleaseDevice(ctx, slot, WithInfiniteRetry(), WithTimeout(devicePoolCloseReleaseTimeout)) — called on pool close

Fix

Replace time.Sleep with a context-aware select:

timer := time.NewTimer(500 * time.Millisecond)
select {
case <-ctx.Done():
    timer.Stop()
    return ctx.Err()
case <-timer.C:
}

This change exists in the local branch fix/nbd-pool-release-retry-ctx but has not been merged to main.

Impact

Delayed orchestrator shutdown when NBD devices fail to release. Under normal conditions (device releases cleanly on first attempt) there is no impact.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in packages/orchestrator/pkg/sandbox/nbd/pool.go around line 369 and read the ReleaseDevice retry loop, then check the callers in path_direct.go:336 and pool.go:395. Confirm that cancellation interrupts the retry delay and returns the context error, including the infinite-retry shutdown paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend, infrastructure
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.