Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions core/changelog.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- fix: recover from idle-timeout timer-goroutine panic that could crash the process
- fix: deterministic MCP tool ordering for prompt cache stability (closes #2347)
35 changes: 35 additions & 0 deletions core/providers/utils/idle_timeout_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ func (panicReader) Read([]byte) (int, error) {
panic(errPanicReader)
}

// timerPanicCloser mimics fasthttp's streaming body when the underlying
// connection has already been released to / reused from the pool: CloseWithError
// nil-derefs in (*HostClient).CloseConn and panics. It implements
// streamCloserWithError (not io.Closer) so closeBodyStream takes the
// CloseWithError branch — the path the idle timer hits.
type timerPanicCloser struct{}

func (timerPanicCloser) Read([]byte) (int, error) { return 0, io.EOF }
func (timerPanicCloser) CloseWithError(error) error { panic("simulated fasthttp CloseConn nil-deref") }

type blockingCloserSpy struct {
started chan struct{}
release chan struct{}
Expand Down Expand Up @@ -391,6 +401,31 @@ func TestIdleTimeoutReader_RecoversReadPanicAfterTimeout(t *testing.T) {
}
}

// TestIdleTimeoutReader_RecoversCloseStreamPanicOnTimerFire verifies that a
// panic raised by closeBodyStream WHEN THE IDLE TIMER FIRES — e.g. fasthttp's
// CloseWithError nil-dereffing in (*HostClient).CloseConn because the stream's
// connection was already released to / reused from the pool (an orphaned timer
// on a completed stream) — is recovered inside the timer goroutine and does not
// crash the process.
//
// This is the timer-callback counterpart to RecoversReadPanicAfterTimeout
// (#3677), which guarded the Read() path but not the AfterFunc's own
// closeBodyStream call. Without the recover in the AfterFunc, the panic runs in
// the timer goroutine, is unrecoverable by callers, and takes the whole process
// down (observed crashing a router under sustained streaming load).
func TestIdleTimeoutReader_RecoversCloseStreamPanicOnTimerFire(t *testing.T) {
t.Parallel()
_, cleanup := NewIdleTimeoutReader(&readCloserSpy{}, timerPanicCloser{}, 10*time.Millisecond, nil)

// Wait past the idle timeout so the timer fires and invokes closeBodyStream,
// which panics. cleanup() then blocks on timerDone, which is only closed
// once the (now-recovered) timer callback has returned — so reaching the end
// of the test proves the panic was swallowed in the timer goroutine. Without
// the recover, the process would have already crashed.
time.Sleep(50 * time.Millisecond)
cleanup()
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

func TestIdleTimeoutReader_CleanupWaitsForRunningTimerCallback(t *testing.T) {
t.Parallel()
body := newBlockingCloserSpy()
Expand Down
8 changes: 8 additions & 0 deletions core/providers/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2308,6 +2308,14 @@ func NewIdleTimeoutReader(reader io.Reader, bodyStream io.Reader, timeout time.D
if ctx != nil {
ctx.SetValue(schemas.BifrostContextKeyConnectionClosed, true)
}
// closeBodyStream may panic: an orphaned timer can fire after the
// stream's connection has already been released to / reused from
// the fasthttp pool, so CloseWithError nil-derefs in
// (*HostClient).CloseConn. Because this runs in the timer goroutine,
// that panic is unrecoverable by callers and crashes the whole
// process. Recover here so a stale idle timer can never take the
// process down (companion to the Read() recover added in #3677).
defer func() { _ = recover() }()
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
closeBodyStream(r.bodyStream, ErrStreamIdleTimeout)
})
})
Expand Down