Fix #4802: Add mutex protection to cancelActiveQuery

This commit is contained in:
Nathan Wallace
2025-11-11 23:16:26 +08:00
parent ff54148eba
commit 7a4772b076
2 changed files with 7 additions and 0 deletions

View File

@@ -55,6 +55,8 @@ type InteractiveClient struct {
// NOTE: should ONLY be called by cancelActiveQueryIfAny // NOTE: should ONLY be called by cancelActiveQueryIfAny
cancelActiveQuery context.CancelFunc cancelActiveQuery context.CancelFunc
cancelPrompt context.CancelFunc cancelPrompt context.CancelFunc
// mutex to protect concurrent access to cancelActiveQuery
cancelMutex sync.Mutex
// channel used internally to pass the initialisation result // channel used internally to pass the initialisation result
initResultChan chan *db_common.InitResult initResultChan chan *db_common.InitResult

View File

@@ -18,11 +18,16 @@ func (c *InteractiveClient) createPromptContext(parentContext context.Context) c
func (c *InteractiveClient) createQueryContext(ctx context.Context) context.Context { func (c *InteractiveClient) createQueryContext(ctx context.Context) context.Context {
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
c.cancelMutex.Lock()
c.cancelActiveQuery = cancel c.cancelActiveQuery = cancel
c.cancelMutex.Unlock()
return ctx return ctx
} }
func (c *InteractiveClient) cancelActiveQueryIfAny() { func (c *InteractiveClient) cancelActiveQueryIfAny() {
c.cancelMutex.Lock()
defer c.cancelMutex.Unlock()
if c.cancelActiveQuery != nil { if c.cancelActiveQuery != nil {
log.Println("[INFO] cancelActiveQueryIfAny CALLING cancelActiveQuery") log.Println("[INFO] cancelActiveQueryIfAny CALLING cancelActiveQuery")
c.cancelActiveQuery() c.cancelActiveQuery()