Stop Watching when there is encoding error

Kubernetes-commit: 79c25d60062d82a107c35c6ae80be926c91f4eca
This commit is contained in:
Ted Yu 2019-11-07 14:32:47 -08:00 committed by Kubernetes Publisher
parent 9ce82fc3a4
commit ea58a61cf5
1 changed files with 3 additions and 5 deletions

View File

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