fix(workflow): prevent concurrent map writes on shared error singletons#2714
Open
nankingjing wants to merge 2 commits into
Open
fix(workflow): prevent concurrent map writes on shared error singletons#2714nankingjing wants to merge 2 commits into
nankingjing wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does
Fixes a
fatal error: concurrent map writescrash in workflow execution (reported in #2713).Root cause
WorkflowErrorvalues carry an error level. The level was stored inside theunderlying
errorx.StatusError'sExtra()map, andChangeErrLevelmutatedthat map in place:
Several
WorkflowErrorvalues are package-level singletons that are shared byevery goroutine:
When a node fails,
(*nodeRunner).onErrorassigns one of these sharedsingletons (e.g.
sErr = vo.NodeTimeoutErroncontext.DeadlineExceeded) andthen calls
sErr.ChangeErrLevel(vo.LevelWarn). Under concurrent workflowexecution — e.g. many requests whose HTTP nodes time out at the same time —
multiple goroutines write to the same shared map simultaneously, which Go
detects as a fatal, unrecoverable
concurrent map writesand crashes thewhole process. This exactly matches the stack trace in #2713
(
node.go:151inChangeErrLevel,node_runner.go:848inonError).The fix
wfErrits ownlevel ErrorLevelfield instead of storing the level inthe shared
Extra()map.ChangeErrLevelnow returns a copy of the error with the new level andnever mutates the receiver, so the shared singletons are never modified.
Level()prefers the new field and falls back to the previousExtra()lookup, so behavior is unchanged for existing callers (
event_handle.go).Extra("level", ...)metadata is keptfor backward compatibility.
After the fix the singletons'
Extra()map is only written once atconstruction and is read-only afterwards, so concurrent access is safe.
Tests
Added
node_concurrency_test.go:TestChangeErrLevelIsSafeForConcurrency— 100 goroutines callChangeErrLevelon the shared singletons concurrently. This reproduces thecrash on the old code and passes on the new code (also clean under
-race).TestChangeErrLevelDoesNotMutateSingleton— verifiesChangeErrLevelreturns a copy and leaves the singleton's level untouched.
Fixes #2713