File tree 2 files changed +29
-0
lines changed
2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -196,6 +196,13 @@ func (p *Pool) getSlow(pid int) any {
196
196
// returns poolLocal pool for the P and the P's id.
197
197
// Caller must call runtime_procUnpin() when done with the pool.
198
198
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
+
199
206
pid := runtime_procPin ()
200
207
// In pinSlow we store to local and then to localSize, here we load in opposite order.
201
208
// Since we've disabled preemption, GC cannot happen in between.
Original file line number Diff line number Diff line change @@ -247,6 +247,28 @@ func testPoolDequeue(t *testing.T, d PoolDequeue) {
247
247
}
248
248
}
249
249
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
+
250
272
func BenchmarkPool (b * testing.B ) {
251
273
var p Pool
252
274
b .RunParallel (func (pb * testing.PB ) {
You can’t perform that action at this time.
0 commit comments