Skip to content

Commit be0e0b0

Browse files
ianlancetaylorgopherbot
authored andcommitted
sync: panic rather than throw on nil *Pool
Fixes #61651 Change-Id: I27d581719e6bf38910f9d47dcf023bbff74ddf72 Reviewed-on: https://go-review.googlesource.com/c/go/+/514037 Reviewed-by: Rob Pike <[email protected]> Reviewed-by: Austin Clements <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]>
1 parent a2905e9 commit be0e0b0

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/sync/pool.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ func (p *Pool) getSlow(pid int) any {
196196
// returns poolLocal pool for the P and the P's id.
197197
// Caller must call runtime_procUnpin() when done with the pool.
198198
func (p *Pool) pin() (*poolLocal, int) {
199+
// Check whether p is nil to get a panic.
200+
// Otherwise the nil dereference happens while the m is pinned,
201+
// causing a fatal error rather than a panic.
202+
if p == nil {
203+
panic("nil Pool")
204+
}
205+
199206
pid := runtime_procPin()
200207
// In pinSlow we store to local and then to localSize, here we load in opposite order.
201208
// Since we've disabled preemption, GC cannot happen in between.

src/sync/pool_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,28 @@ func testPoolDequeue(t *testing.T, d PoolDequeue) {
247247
}
248248
}
249249

250+
func TestNilPool(t *testing.T) {
251+
catch := func() {
252+
if recover() == nil {
253+
t.Error("expected panic")
254+
}
255+
}
256+
257+
var p *Pool
258+
t.Run("Get", func(t *testing.T) {
259+
defer catch()
260+
if p.Get() != nil {
261+
t.Error("expected empty")
262+
}
263+
t.Error("should have panicked already")
264+
})
265+
t.Run("Put", func(t *testing.T) {
266+
defer catch()
267+
p.Put("a")
268+
t.Error("should have panicked already")
269+
})
270+
}
271+
250272
func BenchmarkPool(b *testing.B) {
251273
var p Pool
252274
b.RunParallel(func(pb *testing.PB) {

0 commit comments

Comments
 (0)