From f755769b63c5b0f6eb98a73807858050d7de7ff7 Mon Sep 17 00:00:00 2001 From: Haitao Chen Date: Wed, 7 Sep 2022 11:49:48 -0700 Subject: [PATCH] fix mutex gap (#20) --- task.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/task.go b/task.go index acf4f96..de48898 100644 --- a/task.go +++ b/task.go @@ -143,13 +143,13 @@ func runAndTrackGenericTask[T any](ctx context.Context, record *Task[T], task fu func (t *Task[T]) finish(state State, result *T, err error) { // only update state and result if not yet canceled - if !t.finished() { - t.mutex.Lock() + t.mutex.Lock() + defer t.mutex.Unlock() + if !t.state.IsTerminalState() { t.cancelFunc() // cancel the context t.state = state t.result = result t.err = err - t.mutex.Unlock() } }