From f2ca9c1794617dfdf0db7cf845e84625a6e95b1f Mon Sep 17 00:00:00 2001 From: Sascha Grunert Date: Tue, 1 Jun 2021 17:04:04 +0200 Subject: [PATCH] Fix regression for timed-out stream cleanups If a stream is already timed-out, then either the data or error stream may be `nil`. This would cause a segmentation fault, which is now covered with this patch. Signed-off-by: Sascha Grunert --- .../apimachinery/pkg/util/httpstream/spdy/connection.go | 5 ++++- .../apimachinery/pkg/util/httpstream/spdy/connection_test.go | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/util/httpstream/spdy/connection.go b/staging/src/k8s.io/apimachinery/pkg/util/httpstream/spdy/connection.go index 3da7457f4827..d4ceab84f06d 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/httpstream/spdy/connection.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/httpstream/spdy/connection.go @@ -123,7 +123,10 @@ func (c *connection) Close() error { func (c *connection) RemoveStreams(streams ...httpstream.Stream) { c.streamLock.Lock() for _, stream := range streams { - delete(c.streams, stream.Identifier()) + // It may be possible that the provided stream is nil if timed out. + if stream != nil { + delete(c.streams, stream.Identifier()) + } } c.streamLock.Unlock() } diff --git a/staging/src/k8s.io/apimachinery/pkg/util/httpstream/spdy/connection_test.go b/staging/src/k8s.io/apimachinery/pkg/util/httpstream/spdy/connection_test.go index edef917146fe..143d8c05e3a2 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/httpstream/spdy/connection_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/httpstream/spdy/connection_test.go @@ -323,6 +323,9 @@ func TestConnectionRemoveStreams(t *testing.T) { // remove all existing c.RemoveStreams(stream0, stream1) + // remove nil stream should not crash + c.RemoveStreams(nil) + if len(c.streams) != 0 { t.Fatalf("should not have any streams, has %d", len(c.streams)) }