1
0
mirror of https://github.com/golang/go synced 2024-09-29 23:14:29 -06:00

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 <r@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Ian Lance Taylor 2023-07-29 08:59:20 -07:00 committed by Gopher Robot
parent a2905e95a0
commit be0e0b06ac
2 changed files with 29 additions and 0 deletions

View File

@ -196,6 +196,13 @@ func (p *Pool) getSlow(pid int) any {
// returns poolLocal pool for the P and the P's id.
// Caller must call runtime_procUnpin() when done with the pool.
func (p *Pool) pin() (*poolLocal, int) {
// Check whether p is nil to get a panic.
// Otherwise the nil dereference happens while the m is pinned,
// causing a fatal error rather than a panic.
if p == nil {
panic("nil Pool")
}
pid := runtime_procPin()
// In pinSlow we store to local and then to localSize, here we load in opposite order.
// Since we've disabled preemption, GC cannot happen in between.

View File

@ -247,6 +247,28 @@ func testPoolDequeue(t *testing.T, d PoolDequeue) {
}
}
func TestNilPool(t *testing.T) {
catch := func() {
if recover() == nil {
t.Error("expected panic")
}
}
var p *Pool
t.Run("Get", func(t *testing.T) {
defer catch()
if p.Get() != nil {
t.Error("expected empty")
}
t.Error("should have panicked already")
})
t.Run("Put", func(t *testing.T) {
defer catch()
p.Put("a")
t.Error("should have panicked already")
})
}
func BenchmarkPool(b *testing.B) {
var p Pool
b.RunParallel(func(pb *testing.PB) {