Skip to content

Commit

Permalink
Stop Watching when there is encoding error
Browse files Browse the repository at this point in the history
  • Loading branch information
yutedz committed Nov 7, 2019
1 parent d8ab3f2 commit 639af77
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions staging/src/k8s.io/apiserver/pkg/endpoints/handlers/watch.go
Expand Up @@ -285,10 +285,12 @@ func (s *WatchServer) HandleWS(ws *websocket.Conn) {
buf := &bytes.Buffer{}
streamBuf := &bytes.Buffer{}
ch := s.Watching.ResultChan()

defer s.Watching.Stop()

for {
select {
case <-done:
s.Watching.Stop()
return
case event, ok := <-ch:
if !ok {
Expand Down Expand Up @@ -317,25 +319,21 @@ func (s *WatchServer) HandleWS(ws *websocket.Conn) {
if err != nil {
utilruntime.HandleError(fmt.Errorf("unable to convert watch object: %v", err))
// client disconnect.
s.Watching.Stop()
return
}
if err := s.Encoder.Encode(outEvent, streamBuf); err != nil {
// encoding error
utilruntime.HandleError(fmt.Errorf("unable to encode event: %v", err))
s.Watching.Stop()
return
}
if s.UseTextFraming {
if err := websocket.Message.Send(ws, streamBuf.String()); err != nil {
// Client disconnect.
s.Watching.Stop()
return
}
} else {
if err := websocket.Message.Send(ws, streamBuf.Bytes()); err != nil {
// Client disconnect.
s.Watching.Stop()
return
}
}
Expand Down

0 comments on commit 639af77

Please sign in to comment.