mirror of
https://github.com/vxcontrol/pentagi.git
synced 2026-08-24 10:53:12 -04:00
[PR #126] [CLOSED] fix: prevent goroutine leak and data race in terminal exec timeout #175
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/vxcontrol/pentagi/pull/126
Author: @mason5052
Created: 2/22/2026
Status: ❌ Closed
Base:
master← Head:fix/terminal-exec-goroutine-leak📝 Commits (1)
2c6b7a6fix: prevent goroutine leak and data race in terminal exec timeout📊 Changes
1 file changed (+11 additions, -1 deletions)
View changed files
📝
backend/pkg/tools/terminal.go(+11 -1)📄 Description
Description of the Change
Problem
In
getExecResult()(terminal.go), when command execution times out, three issues occur:Goroutine leak: The goroutine running
io.Copy(&dst, resp.Reader)remains blocked on the reader indefinitely. Whiledefer resp.Close()eventually runs, there is a window where the goroutine is still blocked between the timeout and function return. Under sustained load with many timeouts, this leads to goroutine accumulation and memory bloat.Data race on buffer: On timeout,
dst.String()is called (line 223) while the goroutine may still be writing todstviaio.Copy— a concurrent read/write onbytes.Buffer.Data race on err variable: The goroutine writes to the shared
errvariable (line 216), while the main goroutine also writes toerrin the timeout case (line 224) — a classic data race.Solution
respimmediately on timeout to unblockio.Copy— prevents goroutine leakdst.String()— eliminates buffer data racecopyErrvariable for the goroutine, assigned toerronly after goroutine completes — eliminates err variable data raceThe existing
defer resp.Close()serves as a safety net for the normal (non-timeout) path. Double-close onHijackedResponseis safe.Closes #108
Type of Change
Areas Affected
Testing and Verification
Test Steps
cd backend && go build ./...runtime.NumGoroutine())Security Considerations
Goroutine leaks can lead to resource exhaustion (memory, file descriptors) during sustained penetration testing operations. Fixing this prevents potential DoS conditions when many commands time out simultaneously.
Performance Impact
Positive impact: eliminates goroutine leaks that would otherwise accumulate during long-running pentest sessions with command timeouts.
Checklist
Code Quality
go fmtandgo vet(for Go code)Security
Compatibility
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.