Skip to content

Commit 268033a

Browse files
Aaina26azych
authored andcommitted
Fixed go routine leak in kubelet tests
1 parent 88dfcb2 commit 268033a

File tree

1 file changed

+9
-3
lines changed
  • staging/src/k8s.io/kubelet/pkg/cri/streaming/remotecommand

1 file changed

+9
-3
lines changed

staging/src/k8s.io/kubelet/pkg/cri/streaming/remotecommand/httpstream.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"net/http"
2525
"time"
26+
"context"
2627

2728
api "k8s.io/api/core/v1"
2829
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -116,7 +117,7 @@ func createStreams(req *http.Request, w http.ResponseWriter, opts *Options, supp
116117

117118
if ctx.resizeStream != nil {
118119
ctx.resizeChan = make(chan remotecommand.TerminalSize)
119-
go handleResizeEvents(ctx.resizeStream, ctx.resizeChan)
120+
go handleResizeEvents(req.Context(), ctx.resizeStream, ctx.resizeChan)
120121
}
121122

122123
return ctx, true
@@ -409,7 +410,7 @@ WaitForStreams:
409410
// supportsTerminalResizing returns false because v1ProtocolHandler doesn't support it.
410411
func (*v1ProtocolHandler) supportsTerminalResizing() bool { return false }
411412

412-
func handleResizeEvents(stream io.Reader, channel chan<- remotecommand.TerminalSize) {
413+
func handleResizeEvents(reqctx context.Context, stream io.Reader, channel chan<- remotecommand.TerminalSize) {
413414
defer runtime.HandleCrash()
414415
defer close(channel)
415416

@@ -419,7 +420,12 @@ func handleResizeEvents(stream io.Reader, channel chan<- remotecommand.TerminalS
419420
if err := decoder.Decode(&size); err != nil {
420421
break
421422
}
422-
channel <- size
423+
select {
424+
case channel <- size:
425+
case <-reqctx.Done():
426+
// To prevent go routine leak.
427+
return
428+
}
423429
}
424430
}
425431

0 commit comments

Comments
 (0)