Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

fix: race condition in workflow #1417

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 16 additions & 5 deletions workflow/internal/dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,31 @@ func (d *Dispatcher[S, R]) update(ctx context.Context, id string, status TaskSta
case <-ctx.Done():
return
default:
d.set(id, &status)
if status.Finished() {
}

d.set(id, &status)

if status.Finished() {
d.mu.RLock()
defer d.mu.RUnlock()

select {
case <-d.exitChan:
return
default:
select {
case <-d.exitChan:
return
case d.nextChan <- struct{}{}:
default:
return
default:
}
}
}
}

func (d *Dispatcher[S, R]) stop() {
d.mu.Lock()
defer d.mu.Unlock()

close(d.exitChan)
close(d.nextChan)
}
Expand Down
15 changes: 9 additions & 6 deletions workflow/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ func TestWorkflow(t *testing.T) {
},
State: NewTestState(),
ExpectedErrorMatcher: Not(HaveOccurred()),
ExpectedOrderMatcher: BeEquivalentTo(
[]string{testTaskID1, testTaskID5, testTaskID2, testTaskID3, testTaskID4},
ExpectedOrderMatcher: SatisfyAny(
BeEquivalentTo([]string{testTaskID1, testTaskID5, testTaskID2, testTaskID3, testTaskID4}),
BeEquivalentTo([]string{testTaskID5, testTaskID1, testTaskID2, testTaskID3, testTaskID4}),
),
},
{
Expand Down Expand Up @@ -223,8 +224,9 @@ func TestWorkflow(t *testing.T) {
},
State: NewTestState(),
ExpectedErrorMatcher: HaveOccurred(),
ExpectedOrderMatcher: BeEquivalentTo(
[]string{testTaskID1, testTaskID5, testTaskID2, testTaskID3},
ExpectedOrderMatcher: SatisfyAny(
BeEquivalentTo([]string{testTaskID1, testTaskID5, testTaskID2, testTaskID3}),
BeEquivalentTo([]string{testTaskID5, testTaskID1, testTaskID2, testTaskID3}),
),
},
{
Expand Down Expand Up @@ -279,8 +281,9 @@ func TestWorkflow(t *testing.T) {
State: NewTestState(),
Timeout: 4 * time.Second,
ExpectedErrorMatcher: HaveOccurred(),
ExpectedOrderMatcher: BeEquivalentTo(
[]string{testTaskID1, testTaskID5, testTaskID2, testTaskID3},
ExpectedOrderMatcher: SatisfyAny(
BeEquivalentTo([]string{testTaskID1, testTaskID5, testTaskID2, testTaskID3}),
BeEquivalentTo([]string{testTaskID5, testTaskID1, testTaskID2, testTaskID3}),
),
},
{
Expand Down
Loading