watch: timeout path leaks one goroutine per kill and races on the shared output buffer
まだ誰も着手していません。
評価
調査の方向性
command/watch.go の doWatch と evalCmdOutput から始め、次に提供されている gotify watch の再現手順を race detector で実行します。繰り返される timeout の tick によって goroutine が蓄積せず、child とその出力をコピーする処理が完了した後にのみ出力が読み取られるようになれば、issue は完了です。
索引モデルが issue の本文から書いたものです。
説明
Summary
In watch mode, when the watched command exceeds the interval and is killed via the timeout path, evalCmdOutput leaks a goroutine per timeout occurrence and reads the shared output buffer concurrently with the still-running child pipeline, which is a data race.
Location
- File:
command/watch.go - Function:
doWatch→evalCmdOutput(lines ~86–108)
done := make(chan error) // unbuffered
go func() {
err := cmd.Wait()
if err != nil {
done <- fmt.Errorf("command failed to invoke: %v", err)
}
done <- nil
}()
select {
case err := <-done:
return outputBuf.String(), err
case <-timeOut:
cmd.Process.Kill()
return outputBuf.String(), errors.New("command timed out")
}
Problem
Two defects on the timeout path:
-
Goroutine leak.
doneis unbuffered, and theselectabandons it whentimeOutfires. The spawned goroutine callscmd.Wait(); once the killed process is reaped,Waitreturns and the goroutine attemptsdone <- ...with no receiver ever coming, so it blocks forever. Every interval tick that times out leaks one goroutine (plus the reaped-process bookkeeping), for as long asgotify watchruns — which is by design unbounded (for range time.NewTicker(...).C). -
Data race on
outputBuf. WhenStdout/Stderrare non-*os.Filewriters (here a*bytes.Buffer),os/execspawns internal copy goroutines that write intooutputBuf, and normally onlycmd.Wait()provides the happens-before edge that makes reading the buffer safe. On the timeout pathProcess.Kill()is called withoutWait(), andoutputBuf.String()runs concurrently with those copy goroutines, which may still be draining buffered pipe data from the dying child. ConcurrentBuffer.WriteandBuffer.Stringare not safe.
Trigger / Reproduction
Static analysis finding — not confirmed by execution. Run:
gotify watch -n 1 -- sh -c 'sleep 5; echo done'
Every tick kills sleep 5 after 1 s, taking the case <-timeOut: branch each time: one blocked-forever goroutine per tick, plus an unsynchronized read of outputBuf.
Expected Behavior
The timeout path should reap the child (e.g. call cmd.Wait() after Kill() in a separate goroutine, or use exec.CommandContext with a per-run context so cancellation/reaping is handled by the stdlib) and only then read outputBuf; the completion goroutine should never block on an unreceived channel send (e.g. done := make(chan error, 1)).
Actual Behavior
Goroutines accumulate indefinitely across ticks, and outputBuf can be read while exec's copy goroutines are still writing to it.
Impact
Long-running gotify watch deployments that monitor slow commands accumulate leaked goroutines over time (unbounded memory growth). The unsynchronized buffer access is undefined behavior under the Go memory model and can, under -race, flag crashes/misreads; practically it can observe torn/partial output.
Suggested Direction
Minimal fix: make done buffered (make(chan error, 1)), and after cmd.Process.Kill() call <-done (or cmd.Wait()) before returning outputBuf.String() so the child is fully reaped before its output is read. A cleaner alternative is switching to exec.CommandContext(ctx, ...) per run.
Related but distinct: issue #44 asks for a way to disable the timeout entirely; this report concerns the correctness of the existing timeout implementation itself.
- 主要言語
- Go
- スター
- 588
- フォーク
- 73
- PR マージ指標
- 30日以内にマージされた PR はありません
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
gotify/cli のほかの issue
-
a:feature
-
Package for Ubuntu オープン
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
-
a:feature
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
-
a:feature
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
似ている issue
-
area/dev-productivity area/disaster-recovery area/ipcei kind/enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
kind/bug status/0-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
🤔 refinement needed
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
equinor/radix-operator#1979 ·