Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic on starting child workflows with duplicate IDs #999

Merged
merged 3 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions internal/internal_command_state_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ type (
stateMachineIllegalStatePanic struct {
message string
}

// Error returned when a child workflow with the same id already exists and hasn't completed
// and been removed from internal state.
childWorkflowExistsWithId struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why create a new error instead of using ChildWorkflowExecutionAlreadyStartedError?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a proto - so it doesn't impl the Error interface and feels icky to use in this place

id string
}
)

const (
Expand Down Expand Up @@ -1253,10 +1259,18 @@ func (h *commandsHelper) recordMutableSideEffectMarker(mutableSideEffectID strin
return command
}

func (h *commandsHelper) startChildWorkflowExecution(attributes *commandpb.StartChildWorkflowExecutionCommandAttributes) commandStateMachine {
// startChildWorkflowExecution can return an error in the event that there is already a child wf
// with the same ID which exists as a command in memory. Other SDKs actually will send this command
// to server, and have it reject it - but here the command ID is exactly equal to the child's wf ID,
// and changing that without potentially blowing up backwards compatability is difficult. So we
// return the error eagerly locally, which is at least an improvement on panicking.
func (h *commandsHelper) startChildWorkflowExecution(attributes *commandpb.StartChildWorkflowExecutionCommandAttributes) (commandStateMachine, error) {
command := h.newChildWorkflowCommandStateMachine(attributes)
if h.commands[command.getID()] != nil {
return nil, &childWorkflowExistsWithId{id: attributes.WorkflowId}
Comment on lines +1269 to +1270
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't say I love this, but as is often the case in the Go SDK it seemed like the least offensive of some unfortunate options.

}
h.addCommand(command)
return command
return command, nil
}

func (h *commandsHelper) handleStartChildWorkflowExecutionInitiated(workflowID string) {
Expand Down Expand Up @@ -1513,3 +1527,7 @@ func (h *commandsHelper) isCancelExternalWorkflowEventForChildWorkflow(cancellat
// will have a client generated sequence ID
return len(cancellationID) == 0
}

func (e *childWorkflowExistsWithId) Error() string {
return fmt.Sprintf("child workflow already exists with id: %v", e.id)
}
19 changes: 12 additions & 7 deletions internal/internal_command_state_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ func Test_ChildWorkflowStateMachine_Basic(t *testing.T) {
h := newCommandsHelper()

// start child workflow
d := h.startChildWorkflowExecution(attributes)
d, err := h.startChildWorkflowExecution(attributes)
require.NoError(t, err)
require.Equal(t, commandStateCreated, d.getState())

// send command
Expand Down Expand Up @@ -391,7 +392,8 @@ func Test_ChildWorkflowStateMachine_CancelSucceed(t *testing.T) {
h := newCommandsHelper()

// start child workflow
d := h.startChildWorkflowExecution(attributes)
d, err := h.startChildWorkflowExecution(attributes)
require.NoError(t, err)
// send command
_ = h.getCommands(true)
// child workflow initiated
Expand Down Expand Up @@ -435,11 +437,12 @@ func Test_ChildWorkflowStateMachine_InvalidStates(t *testing.T) {
h := newCommandsHelper()

// start child workflow
d := h.startChildWorkflowExecution(attributes)
d, err := h.startChildWorkflowExecution(attributes)
require.NoError(t, err)
require.Equal(t, commandStateCreated, d.getState())

// invalid: start child workflow failed before command was sent
err := runAndCatchPanic(func() {
err = runAndCatchPanic(func() {
h.handleStartChildWorkflowExecutionFailed(workflowID)
})
require.NotNil(t, err)
Expand Down Expand Up @@ -511,7 +514,8 @@ func Test_ChildWorkflow_UnusualCancelationOrdering(t *testing.T) {
h := newCommandsHelper()

// start child workflow
h.startChildWorkflowExecution(attributes)
_, err := h.startChildWorkflowExecution(attributes)
require.NoError(t, err)
// send command
h.getCommands(true)
// child workflow initiated
Expand All @@ -525,7 +529,7 @@ func Test_ChildWorkflow_UnusualCancelationOrdering(t *testing.T) {
// Now, the unusual part. The cancellation happens before we get the external cancel request
h.handleChildWorkflowExecutionCanceled(workflowID)
// Oh no, server took a bit.
err := runAndCatchPanic(func() {
err = runAndCatchPanic(func() {
h.handleExternalWorkflowExecutionCancelRequested(initiatedEventID, workflowID)
})
require.Nil(t, err)
Expand All @@ -544,7 +548,8 @@ func Test_ChildWorkflowStateMachine_CancelFailed(t *testing.T) {
h := newCommandsHelper()

// start child workflow
d := h.startChildWorkflowExecution(attributes)
d, err := h.startChildWorkflowExecution(attributes)
require.NoError(t, err)
// send command
h.getCommands(true)
// child workflow initiated
Expand Down
6 changes: 5 additions & 1 deletion internal/internal_event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,11 @@ func (wc *workflowEnvironmentImpl) ExecuteChildWorkflow(
attributes.CronSchedule = params.CronSchedule
}

command := wc.commandsHelper.startChildWorkflowExecution(attributes)
command, err := wc.commandsHelper.startChildWorkflowExecution(attributes)
if _, ok := err.(*childWorkflowExistsWithId); ok {
callback(nil, &ChildWorkflowExecutionAlreadyStartedError{})
return
}
command.setData(&scheduledChildWorkflow{
resultCallback: callback,
startedCallback: startedHandler,
Expand Down
13 changes: 10 additions & 3 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,9 +999,16 @@ func (ts *IntegrationTestSuite) TestCancelChildWorkflowUnusualTransitions() {
)
ts.NoError(err)

// Synchronously wait for the workflow completion. Behind the scenes the SDK performs a long poll operation.
// If you need to wait for the workflow completion from another process use
// Client.GetWorkflow API to get an instance of a WorkflowRun.
err = run.Get(context.Background(), nil)
ts.NoError(err)
}

func (ts *IntegrationTestSuite) TestChildWorkflowDuplicatePanic_Regression() {
wfid := "test-child-workflow-duplicate-panic-regression"
run, err := ts.client.ExecuteWorkflow(context.Background(),
ts.startWorkflowOptions(wfid),
ts.workflows.ChildWorkflowDuplicatePanicRepro)
ts.NoError(err)
err = run.Get(context.Background(), nil)
ts.NoError(err)
}
Expand Down
26 changes: 25 additions & 1 deletion test/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,29 @@ func (w *Workflows) ChildWorkflowCancelUnusualTransitionsRepro(ctx workflow.Cont
return nil
}

func (w *Workflows) ChildWorkflowDuplicatePanicRepro(ctx workflow.Context) error {
cwo := workflow.ChildWorkflowOptions{
WorkflowID: "ABC-SIMPLE-CHILD-WORKFLOW-ID",
}
childCtx := workflow.WithChildOptions(ctx, cwo)

child1 := workflow.ExecuteChildWorkflow(childCtx, w.childWorkflowWaitOnSignal)
var childWE workflow.Execution
err := child1.GetChildWorkflowExecution().Get(ctx, &childWE)
if err != nil {
return err
}
workflow.SignalExternalWorkflow(ctx, childWE.ID, childWE.RunID, "unblock", nil)
if err != nil {
return err
}
err = workflow.ExecuteChildWorkflow(childCtx, w.childWorkflowWaitOnSignal).Get(ctx, nil)
if _, ok := err.(*internal.ChildWorkflowExecutionAlreadyStartedError); !ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we expose this error outside of internal?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, so, it is one of the many things that's aliased. I can't refer to the temporal version of it here b/c circular import. It's aliased here:

ChildWorkflowExecutionAlreadyStartedError = internal.ChildWorkflowExecutionAlreadyStartedError

I don't track the whole aliasing stuff so if I need to somehow return it differently I'm all ears.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come other errors can be used from temporal https://github.com/temporalio/sdk-go/blob/master/test/workflow_test.go#L220 but this one can't?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hahah, derp. I didn't realize this comment was on the test file. Fixed.

panic("Second child must fail to start as duplicate")
}
return nil
}

func (w *Workflows) ActivityCancelRepro(ctx workflow.Context) ([]string, error) {
ctx, cancelFunc := workflow.WithCancel(ctx)

Expand Down Expand Up @@ -1860,7 +1883,7 @@ func (w *Workflows) HeartbeatSpecificCount(ctx workflow.Context, interval time.D
func (w *Workflows) UpsertMemo(ctx workflow.Context, memo map[string]interface{}) (*commonpb.Memo, error) {
err := workflow.UpsertMemo(ctx, memo)
if err != nil {
return nil, err;
return nil, err
}
return workflow.GetInfo(ctx).Memo, nil
}
Expand Down Expand Up @@ -1897,6 +1920,7 @@ func (w *Workflows) register(worker worker.Worker) {
worker.RegisterWorkflow(w.ChildWorkflowSuccessWithParentClosePolicyTerminate)
worker.RegisterWorkflow(w.ChildWorkflowSuccessWithParentClosePolicyAbandon)
worker.RegisterWorkflow(w.ChildWorkflowCancelUnusualTransitionsRepro)
worker.RegisterWorkflow(w.ChildWorkflowDuplicatePanicRepro)
worker.RegisterWorkflow(w.ConsistentQueryWorkflow)
worker.RegisterWorkflow(w.ContextPropagator)
worker.RegisterWorkflow(w.ContinueAsNew)
Expand Down