Skip to content

fix(core): make JobResult.Finish idempotent - #491

Merged
mudler merged 1 commit into
mudler:mainfrom
walcz-de:fix/jobresult-finish-idempotent
Sep 27, 2026
Merged

mudler merged 1 commit into
mudler:mainfrom
walcz-de:fix/jobresult-finish-idempotent

Conversation

@walcz-de

@walcz-de walcz-de commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Problem

JobResult.Finish unconditionally closes the ready channel. A job can legitimately be finished from more than one code path — an early finish inside a tool decision, followed by the regular completion of the job. The second call hits close(j.ready) on an already closed channel and panics with close of closed channel, which brings the whole agent down.

Reproduction

j := types.NewJobResult()
j.Finish(errors.New("first"))
j.Finish(nil) // panic: close of closed channel

The new test TestJobResultFinishIsIdempotent in core/types/result_finish_test.go reproduces this; it panics on main and passes with this change.

Fix

Finish is now idempotent. Under the job lock it checks whether ready is already closed and returns early if so. The first call wins: its error is stored, the channel is closed once, and the finalizers run exactly once. Later calls are no-ops. WaitResult and all other callers are unaffected.

Testing

  • go test ./core/types/... — fails (panic) without the fix, passes with it
  • go vet ./core/types/ — clean

🤖 Generated with Claude Code

https://claude.ai/code/session_01PHcZ8CTpZEoK3qgroNnkB4

A job can be finished from more than one code path: an early finish inside
a tool decision, followed by the regular completion. The second Finish
closed the already-closed ready channel and panicked with
"close of closed channel", taking the whole agent down.

Finish now checks whether the ready channel is already closed (under the
job lock) and returns early. The first call wins: its error is stored and
its finalizers run exactly once; later calls are no-ops.

Adds a regression test that finishes a job twice.

Signed-off-by: Stefan Walcz <stefan.walcz@walcz.de>
@mudler
mudler merged commit 6e34206 into mudler:main Sep 27, 2026
@mudler-agent

Copy link
Copy Markdown

Merged into main in f2a2af4, together with the other PRs in this batch (branch merge/pr-batch-20260927). Thanks!

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.

3 participants