Skip to content

fix(envd): close fds inherited across a live upgrade - #3843

Open
SebTardif wants to merge 1 commit into
e2b-dev:mainfrom
SebTardif:fix/envd-readopt-close-fds
Open

SebTardif wants to merge 1 commit into
e2b-dev:mainfrom
SebTardif:fix/envd-readopt-close-fds

Conversation

@SebTardif

Copy link
Copy Markdown

Summary

After an envd live upgrade, file descriptors for processes that were already running stay open after those processes exit.

What happens

Readopt starts pump on the inherited stdout, stderr, and pty. pump returned on EOF without closing the *os.File. reapReadopted never closed the stdin write end either.

A process started in the current envd does not do this. Its reader goroutines defer Close on the read ends, and Handler.Wait calls closeStdinPipe before it publishes the exit.

The descriptors are CLOEXEC, so they are not inherited by child processes. They stay inside envd until the os.File finalizer runs. A sandbox that upgrades while many processes are alive, then lets those processes exit, keeps one descriptor per stream until garbage collection.

pump now closes an *os.File when that reader returns. The reaper closes stdin before it sends the terminal event, the same order as Wait. A later handover reads a closed file as fd -1, and dupKeep already treats that as absent. That matches a fresh-start process whose reader has already finished.

Tests

TestPumpClosesInheritedFileOnEOF and TestReapReadoptedClosesStdin.

Before the close, both failed because Stat on the still-open pipe succeeded:

--- FAIL: TestPumpClosesInheritedFileOnEOF
    Error: An error is expected but got nil.
--- FAIL: TestReapReadoptedClosesStdin
    Error: An error is expected but got nil.

After the close, go test of packages/envd/internal/services/process/handler on linux/amd64 passed, including these two tests and the existing package tests.

Origin

Introduced in #3333 (47a7cc8f3e, 2026-07-28).

Re-adopted readers returned on EOF without closing the stdout, stderr,
or pty file. The reaper also left the stdin write end open. Fresh-start
readers and Wait already close those ends, so a process that exits after
an upgrade kept the descriptors in envd until the runtime finalizer ran.
@cla-bot

cla-bot Bot commented Dec 2, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @SebTardif on file. You can sign our CLA at https://e2b.dev/docs/cla . Once you've signed, post a comment here that says '@cla-bot check'

@SebTardif

Copy link
Copy Markdown
Author

@cla-bot check

@cla-bot cla-bot Bot added the cla-signed label Dec 2, 2026
@cla-bot

cla-bot Bot commented Dec 2, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant