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

internal/poll: ensure that newPoolPipe doesn't return a nil pointer

The function could occasionally return a nil pointer as a non-nil
interface, confusing the calling code.

Fixes #45520

Change-Id: Ifd35613728efa2cee9903177e85d369155074804
Reviewed-on: https://go-review.googlesource.com/c/go/+/309429
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Andy Pan <panjf2000@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Ian Lance Taylor 2021-04-12 10:02:37 -07:00
parent 2fa7163b06
commit 8b859be9c3
2 changed files with 17 additions and 4 deletions

View File

@ -169,9 +169,10 @@ func newPoolPipe() interface{} {
// Discard the error which occurred during the creation of pipe buffer,
// redirecting the data transmission to the conventional way utilizing read() + write() as a fallback.
p := newPipe()
if p != nil {
runtime.SetFinalizer(p, destroyPipe)
if p == nil {
return nil
}
runtime.SetFinalizer(p, destroyPipe)
return p
}

View File

@ -75,13 +75,19 @@ func TestSplicePipePool(t *testing.T) {
func BenchmarkSplicePipe(b *testing.B) {
b.Run("SplicePipeWithPool", func(b *testing.B) {
for i := 0; i < b.N; i++ {
p, _, _ := poll.GetPipe()
p, _, err := poll.GetPipe()
if err != nil {
continue
}
poll.PutPipe(p)
}
})
b.Run("SplicePipeWithoutPool", func(b *testing.B) {
for i := 0; i < b.N; i++ {
p := poll.NewPipe()
if p == nil {
b.Skip("newPipe returned nil")
}
poll.DestroyPipe(p)
}
})
@ -90,7 +96,10 @@ func BenchmarkSplicePipe(b *testing.B) {
func BenchmarkSplicePipePoolParallel(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
p, _, _ := poll.GetPipe()
p, _, err := poll.GetPipe()
if err != nil {
continue
}
poll.PutPipe(p)
}
})
@ -100,6 +109,9 @@ func BenchmarkSplicePipeNativeParallel(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
p := poll.NewPipe()
if p == nil {
b.Skip("newPipe returned nil")
}
poll.DestroyPipe(p)
}
})