Skip to content

fix: prevent goroutine leak in terminal exec timeout handling#139

Closed
haosenwang1018 wants to merge 1 commit intovxcontrol:masterfrom
haosenwang1018:fix/goroutine-leak-exec-timeout
Closed

fix: prevent goroutine leak in terminal exec timeout handling#139
haosenwang1018 wants to merge 1 commit intovxcontrol:masterfrom
haosenwang1018:fix/goroutine-leak-exec-timeout

Conversation

@haosenwang1018
Copy link
Contributor

Problem

When getExecResult times out via ctx.Done(), the function returns but leaves a goroutine blocked on io.Copy(&dst, resp.Reader). The connection (resp) is only closed by defer after the function returns, but by that point the goroutine is already leaked — io.Copy may remain blocked even after resp.Close() because the close races with the blocked read.

Each timed-out command leaks one goroutine and its associated resources (#108).

Fix

  • Close resp before waiting for the goroutine on timeout, which unblocks io.Copy
  • Wait for the goroutine (<-done) after closing to ensure clean shutdown
  • On the normal path, close resp immediately after copy completes
  • Remove defer resp.Close() since both paths now close explicitly

Before

timeout → return → defer resp.Close() → goroutine still blocked on io.Copy

After

timeout → resp.Close() → io.Copy returns → <-done → return (no leak)

Fixes #108

When getExecResult times out, the goroutine running io.Copy remains
blocked on resp.Reader indefinitely because resp is only closed via
defer after the function returns. This leaks one goroutine per
timed-out command.

Fix: close resp.Conn explicitly on timeout to unblock io.Copy, then
wait for the goroutine to finish before returning. On the normal
(non-timeout) path, close resp immediately after the copy completes.

Fixes vxcontrol#108
@asdek
Copy link
Contributor

asdek commented Feb 23, 2026

hey @haosenwang1018

these changes was included by #124 into temporary branch
https://github.com/vxcontrol/pentagi/commits/feature/project_improvements/

it'll be added today in the master branch

thank you for suggestion!

@asdek asdek closed this Feb 23, 2026
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] Goroutine and Resource Leak in Terminal Exec Timeout Handling

2 participants