Skip to content

Commit 142ad22

Browse files
Copilotasmyasnikov
andauthored
Fix race condition in pool item creation causing flaky TestDoCreateSessionError (#1905)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: asmyasnikov <[email protected]>
1 parent 51e67c3 commit 142ad22

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

internal/pool/pool.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,20 @@ func makeAsyncCreateItemFunc[PT ItemConstraint[T], T any]( //nolint:funlen
276276
case <-p.done:
277277
return nil, xerrors.WithStackTrace(errClosedPool)
278278
case <-ctx.Done():
279+
// Try non-blocking read from ch to check if goroutine has already completed
280+
select {
281+
case result, has := <-ch:
282+
if has {
283+
if result.err != nil {
284+
// Goroutine completed with an error, join it with context error
285+
return nil, xerrors.WithStackTrace(xerrors.Join(result.err, ctx.Err()))
286+
}
287+
// Goroutine completed successfully, return the item
288+
return result.item, nil
289+
}
290+
default:
291+
}
292+
279293
return nil, xerrors.WithStackTrace(ctx.Err())
280294
case result, has := <-ch:
281295
if !has {

0 commit comments

Comments
 (0)