Skip to content

fix(workflow): prevent concurrent map read/write panic during stream processing#2653

Open
phpoh wants to merge 1 commit intocoze-dev:mainfrom
phpoh:fix-concurrent-map-read-write-panic
Open

fix(workflow): prevent concurrent map read/write panic during stream processing#2653
phpoh wants to merge 1 commit intocoze-dev:mainfrom
phpoh:fix-concurrent-map-read-write-panic

Conversation

@phpoh
Copy link
Copy Markdown

@phpoh phpoh commented Apr 7, 2026

Summary

  • Prevents "concurrent map iteration and map write" panics during workflow stream processing
  • Removes single-chunk shortcuts that returned shared map references directly
  • Always uses ConcatMaps to create deep copies of map data

Problem

When processing streaming output chunks, the code directly used shared map references without deep copying. This caused "concurrent map iteration and map write" panics when other goroutines were still modifying the original maps during JSON marshaling.

The panic occurred in mustMarshalToString when serializing event.Output while the original map was still being modified elsewhere.

Changes

  1. In buildStreamEndEvent: Removed single-chunk shortcut that returned shared map reference directly
  2. In ConcatStructuredCallbackOutputs: Removed single-element shortcuts for inputList and extraList
  3. In ConcatStructuredCallbackInputs: Removed single-input shortcut and extraList shortcut

All map data is now processed through ConcatMaps which creates deep copies, preventing concurrent access issues.

Test plan

  • Build passes: go build ./domain/workflow/internal/execute/... ./domain/workflow/internal/nodes/...
  • Unit tests pass for the modified packages
  • Manual testing with concurrent workflow execution (recommended by reviewer)

Fixes #2607

🤖 Generated with Claude Code

…processing

When processing streaming output chunks, the code directly used shared
map references without deep copying. This caused "concurrent map iteration
and map write" panics when other goroutines were still modifying the
original maps during JSON marshaling.

Changes:
- Remove single-chunk shortcut in buildStreamEndEvent that returned
  shared map reference directly
- Remove single-element shortcuts in ConcatStructuredCallbackOutputs
  and ConcatStructuredCallbackInputs that returned shared references
- Always use ConcatMaps to create deep copies of map data, preventing
  concurrent access issues

This fixes the panic reported in issue coze-dev#2607.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

服务 panic:"fatal error: concurrent map iteration and map write"

1 participant