Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ianlancetaylor committed Apr 12, 2021
1 parent 2fa7163 commit 8b859be
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/internal/poll/splice_linux.go
Expand Up @@ -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
}

Expand Down
16 changes: 14 additions & 2 deletions src/internal/poll/splice_linux_test.go
Expand Up @@ -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)
}
})
Expand All @@ -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)
}
})
Expand All @@ -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)
}
})
Expand Down

0 comments on commit 8b859be

Please sign in to comment.