Merge pull request #2219 from beck-8/fix/context-done-race

fix: avoid data race when watching request cancellation
This commit is contained in:
Luis Pater
2026-03-23 22:57:21 +08:00
committed by GitHub

View File

@@ -339,12 +339,13 @@ func (h *BaseAPIHandler) GetContextWithCancel(handler interfaces.APIHandler, c *
} }
} }
newCtx, cancel := context.WithCancel(parentCtx) newCtx, cancel := context.WithCancel(parentCtx)
cancelCtx := newCtx
if requestCtx != nil && requestCtx != parentCtx { if requestCtx != nil && requestCtx != parentCtx {
go func() { go func() {
select { select {
case <-requestCtx.Done(): case <-requestCtx.Done():
cancel() cancel()
case <-newCtx.Done(): case <-cancelCtx.Done():
} }
}() }()
} }