Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/envd/internal/services/process/handler/readopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ func (p *Handler) BeginReaping() {
// pump mirrors the New() reader loops: read a chunk, account it, and fan it out
// to subscribers (dropped if none — same semantics as the live path).
func (p *Handler) pump(r io.Reader, chunk int, counter *atomic.Int64, mk func([]byte) *rpc.ProcessEvent_DataEvent) {
// Fresh-start readers close their own pipe ends. These files were inherited
// across execve, so this reader is the owner.
if f, ok := r.(*os.File); ok {
defer f.Close()
}

buf := make([]byte, chunk)
for {
n, readErr := r.Read(buf)
Expand All @@ -184,6 +190,10 @@ func (p *Handler) pump(r io.Reader, chunk int, counter *atomic.Int64, mk func([]
func (p *Handler) reapReadopted() {
end := p.waitReadopted()

// Wait closes the stdin write end before it publishes the exit. The read
// ends are closed by pump when those streams finish.
p.closeStdinPipe()

p.EndEvent.Source <- rpc.ProcessEvent_End{End: end}
// Retain the terminal event synchronously — before closing the source — so a
// Connect that forks after the close and falls back to the retention cache is
Expand Down
61 changes: 61 additions & 0 deletions packages/envd/internal/services/process/handler/readopt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
package handler

import (
"os"
"sync/atomic"
"testing"
"time"

"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

rpc "github.com/e2b-dev/infra/packages/envd/internal/services/spec/process"
)

// TestReapByPidfd_EmitsEndWhenPidfdOpenFails guards against orphaning a
Expand Down Expand Up @@ -65,3 +69,60 @@ func TestBeginReaping_ArmsDeadlineForChainedUpgrade(t *testing.T) {
d, _ := h.Deadline()
assert.WithinDuration(t, time.Now().Add(time.Hour), d, time.Minute)
}

// TestPumpClosesInheritedFileOnEOF is the fresh-start contract for a
// re-adopted stream: the reader goroutine closes its own read end when the
// write end is gone. Leaving it open keeps the inherited fd in envd until
// the os.File finalizer runs.
func TestPumpClosesInheritedFileOnEOF(t *testing.T) {
t.Parallel()

r, w, err := os.Pipe()
require.NoError(t, err)
t.Cleanup(func() {
_ = r.Close()
_ = w.Close()
})
require.NoError(t, w.Close())

var n atomic.Int64
h := &Handler{}
h.pump(r, 32, &n, func(b []byte) *rpc.ProcessEvent_DataEvent {
return &rpc.ProcessEvent_DataEvent{Output: &rpc.ProcessEvent_DataEvent_Stdout{Stdout: b}}
})

_, err = r.Stat()
require.Error(t, err, "pump left the inherited read end open after EOF")
}

// TestReapReadoptedClosesStdin closes the write end the fresh-start Wait
// path releases in closeStdinPipe. The reaper must do that before it
// publishes the terminal event, so a client that observes the exit cannot
// still be holding envd's copy of the pipe.
func TestReapReadoptedClosesStdin(t *testing.T) {
t.Parallel()

logger := zerolog.Nop()
r, w, err := os.Pipe()
require.NoError(t, err)
t.Cleanup(func() {
_ = r.Close()
_ = w.Close()
})

h := Readopt(ReadoptArgs{Pid: 0x7FFFFFFF, Stdin: w}, &logger)
endCh, cancel := h.EndEvent.Fork()
defer cancel()

h.BeginReaping()

select {
case _, ok := <-endCh:
require.True(t, ok, "EndEvent channel closed without a terminal event")
case <-time.After(5 * time.Second):
require.Fail(t, "no EndEvent for a re-adopted process that cannot exist")
}

_, err = w.Stat()
require.Error(t, err, "reaper left the inherited stdin open after the process ended")
}