From 749af2505e1c79edbfe6510e3a587b05ef6c2309 Mon Sep 17 00:00:00 2001 From: Marco Brotas Date: Wed, 16 Oct 2024 09:22:33 +0200 Subject: [PATCH] fix: race condition on async list await --- async.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/async.go b/async.go index e0063fc..9389118 100644 --- a/async.go +++ b/async.go @@ -1,7 +1,10 @@ package command +import "sync" + // Async is the struct returned from async commands. type Async struct { + sync.Mutex hdl Handler cmd Command data any @@ -55,14 +58,18 @@ func (as *Async) success(data any) { } func (as *Async) setListener(listener func(as *Async)) { + as.Lock() as.listener = listener + as.Unlock() if as.done.enabled() { as.notifyListener() } } func (as *Async) notifyListener() { + as.Lock() if as.listener != nil { as.listener(as) } + as.Unlock() }