Skip to content

fix(runner): prevent panic when timeout races with process completion - #288

Open
Souravrajvi0 wants to merge 1 commit into
langgenius:mainfrom
Souravrajvi0:feat/fix-output-capture-timer-race-5c00
Open

Souravrajvi0 wants to merge 1 commit into
langgenius:mainfrom
Souravrajvi0:feat/fix-output-capture-timer-race-5c00

Conversation

@Souravrajvi0

Copy link
Copy Markdown

Summary

Fixes #284

Under high concurrency, when code execution finishes near WORKER_TIMEOUT, the time.AfterFunc timeout callback could still send on output channels after the normal completion path had finished. This caused panic: send on closed channel and crashed the entire daemon process.

Root cause

timer.Stop() does not prevent an already-scheduled timer callback from executing. When the python process exits right around the timeout deadline, the completion goroutine and the timer callback race — the callback may send on execError after channels are drained/closed.

Changes

  • Timer synchronization: If timer.Stop() returns false (timer already fired), wait for the timer callback to finish via sync.WaitGroup before signaling done.
  • Timeout guard: Use sync.Once so the timeout error is written at most once.
  • Safe channel sends: Wrap all output channel writes with recover() to prevent panics if a channel is already closed (defensive safety net, as suggested in the issue).

Test plan

  • TestCaptureOutputTimeoutRaceDoesNotPanic — 200 iterations with execution time straddling the timeout deadline
  • TestSendBytesRecoversFromClosedChannel — verifies closed-channel sends do not panic
  • All existing TestCaptureOutput* tests pass

When execution finishes near WORKER_TIMEOUT, the timer callback could
still send on output channels after the completion path had already
finished, causing "send on closed channel" and crashing the daemon.

Wait for an already-fired timer callback before signaling done, guard
the timeout error with sync.Once, and make channel sends recover from
closed-channel panics as a safety net.

Fixes langgenius#284
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: panic "send on closed channel" in OutputCaptureRunner when execution time approaches WORKER_TIMEOUT

1 participant