Skip to content

Commit 6d38d3f

Browse files
committed
fix race in step name map
1 parent 555c367 commit 6d38d3f

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

dbos/workflow.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"math"
99
"reflect"
1010
"runtime"
11+
"sync"
1112
"time"
1213

1314
"github.com/google/uuid"
@@ -705,7 +706,7 @@ func setStepParamDefaults(params *StepParams, stepName string) *StepParams {
705706
BackoffFactor: 2.0,
706707
BaseInterval: 100 * time.Millisecond, // Default base interval
707708
MaxInterval: 5 * time.Second, // Default max interval
708-
StepName: typeErasedStepNameToStepName[stepName],
709+
StepName: getTypeErasedStepName(stepName),
709710
}
710711
}
711712

@@ -721,13 +722,22 @@ func setStepParamDefaults(params *StepParams, stepName string) *StepParams {
721722
}
722723
if params.StepName == "" {
723724
// If the step name is not provided, use the function name
724-
params.StepName = typeErasedStepNameToStepName[stepName]
725+
params.StepName = getTypeErasedStepName(stepName)
725726
}
726727

727728
return params
728729
}
729730

730-
var typeErasedStepNameToStepName = make(map[string]string)
731+
var (
732+
typeErasedStepNameToStepName = make(map[string]string)
733+
typeErasedStepNameMutex sync.RWMutex
734+
)
735+
736+
func getTypeErasedStepName(stepName string) string {
737+
typeErasedStepNameMutex.RLock()
738+
defer typeErasedStepNameMutex.RUnlock()
739+
return typeErasedStepNameToStepName[stepName]
740+
}
731741

732742
func RunAsStep[R any](ctx DBOSContext, fn GenericStepFunc[R]) (R, error) {
733743
if ctx == nil {
@@ -742,7 +752,10 @@ func RunAsStep[R any](ctx DBOSContext, fn GenericStepFunc[R]) (R, error) {
742752

743753
// Type-erase the function
744754
typeErasedFn := StepFunc(func(ctx context.Context) (any, error) { return fn(ctx) })
745-
typeErasedStepNameToStepName[runtime.FuncForPC(reflect.ValueOf(typeErasedFn).Pointer()).Name()] = stepName
755+
typeErasedFnName := runtime.FuncForPC(reflect.ValueOf(typeErasedFn).Pointer()).Name()
756+
typeErasedStepNameMutex.Lock()
757+
typeErasedStepNameToStepName[typeErasedFnName] = stepName
758+
typeErasedStepNameMutex.Unlock()
746759

747760
// Call the executor method and pass through the result/error
748761
result, err := ctx.RunAsStep(ctx, typeErasedFn)

0 commit comments

Comments
 (0)