Skip to content

Commit c2d5e5f

Browse files
committed
add spawnCtx cancelation during pool closing
1 parent 028fa82 commit c2d5e5f

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

internal/pool/pool.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ type (
6060
done chan struct{}
6161

6262
stats *safeStats
63+
64+
spawnCancel context.CancelFunc
6365
}
6466
option[PT Item[T], T any] func(p *Pool[PT, T])
6567
)
@@ -202,7 +204,9 @@ func New[PT Item[T], T any](
202204
onChange: p.trace.OnChange,
203205
}
204206

205-
go p.spawnItems(xcontext.ValueOnly(ctx))
207+
var spawnCtx context.Context
208+
spawnCtx, p.spawnCancel = xcontext.WithCancel(xcontext.ValueOnly(ctx))
209+
go p.spawnItems(spawnCtx)
206210

207211
return p
208212
}
@@ -512,6 +516,9 @@ func (p *Pool[PT, T]) Close(ctx context.Context) (finalErr error) {
512516
})
513517
}()
514518

519+
// canceling spawner (and any underlying createItem calls)
520+
p.spawnCancel()
521+
515522
// Only closing done channel.
516523
// Due to multiple senders queue is not closed here,
517524
// we're just making sure to drain it fully to close any existing item.

0 commit comments

Comments
 (0)