Change:
func TestDecoratedWatcher(t *testing.T) { w := watch.NewFake() decorator := func(obj runtime.Object) { <del> pod := obj.(*example.Pod) pod.Annotations = map[string]string{"decorated": "true"} </del> <ins> if pod, ok := obj.(*example.Pod); ok { pod.Annotations = map[string]string{"decorated": "true"} } </ins> } <del> dw := newDecoratedWatcher(w, decorator) </del> <ins> ctx, cancel := context.WithCancel(context.Background()) dw := newDecoratedWatcher(ctx, w, decorator) </ins> defer dw.Stop() <del> go w.Add(&example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) </del> <ins> go func() { w.Error(&metav1.Status{Status: "Failure"}) w.Add(&example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) w.Error(&metav1.Status{Status: "Failure"}) w.Modify(&example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) w.Error(&metav1.Status{Status: "Failure"}) w.Delete(&example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) }() expectErrorEvent(t, dw) // expect error is plumbed and doesn't force close the watcher expectPodEvent(t, dw, watch.Added) expectErrorEvent(t, dw) // expect error is plumbed and doesn't force close the watcher expectPodEvent(t, dw, watch.Modified) expectErrorEvent(t, dw) // expect error is plumbed and doesn't force close the watcher expectPodEvent(t, dw, watch.Deleted) // cancel the passed-in context to simulate request timeout cancel() // expect the decorated channel to be closed select { case e, ok := <-dw.ResultChan(): if ok { t.Errorf("expected result chan closed, got %#v", e) } case <-time.After(wait.ForeverTestTimeout): t.Errorf("timeout after %v", wait.ForeverTestTimeout) } // expect the underlying watcher to have been stopped as a result of the context cancellation if !w.IsStopped() { t.Errorf("expected underlying watcher to be stopped") } } func expectPodEvent(t *testing.T, dw *decoratedWatcher, watchType watch.EventType) { </ins> select { case e := <-dw.ResultChan(): pod, ok := e.Object.(*example.Pod) if !ok { <del> t.Errorf("Should received object of type *api.Pod, get type (%T)", e.Object) return </del> <ins> t.Fatalf("Should received object of type *api.Pod, get type (%T)", e.Object) </ins> } if pod.Annotations["decorated"] != "true" { <del> t.Errorf("pod.Annotations["decorated"], want=%s, get=%s", "true", pod.Labels["decorated"]) </del> <ins> t.Fatalf("pod.Annotations["decorated"], want=%s, get=%s", "true", pod.Labels["decorated"]) } if e.Type != watchType { t.Fatalf("expected type %s, got %s", watchType, e.Type) </ins> } case <-time.After(wait.ForeverTestTimeout): <del> t.Errorf("timeout after %v", wait.ForeverTestTimeout) </del> <ins> t.Fatalf("timeout after %v", wait.ForeverTestTimeout) } } func expectErrorEvent(t *testing.T, dw *decoratedWatcher) { select { case e := <-dw.ResultChan(): _, ok := e.Object.(*metav1.Status) if !ok { t.Fatalf("Should received object of type *metav1.Status, get type (%T)", e.Object) } if e.Type != watch.Error { t.Fatalf("expected type %s, got %s", watch.Error, e.Type) } case <-time.After(wait.ForeverTestTimeout): t.Fatalf("timeout after %v", wait.ForeverTestTimeout) </ins> } }

Discussion:
Using the Fabric8 client, which defaults to using WebSockets for Watches, I established a Watch on Service resources. After the timeout (implicit or explicit) rather than the WebSocket seeing a close control frame or even an EOF it continued to be open and fulfill ping/pong requests - but no Service events are sent. The WebSocket would close. Create a WebSocket based Watch with the ListOptions timeoutSeconds set on the Service resource along with a steady production of Service events, such as create/delete of a dummy Service. Kubernetes version (use ): Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.0", GitCommit:"", GitTreeState:"clean", BuildDate:"2020-10-27T05:15:27Z", GoVersion:"go1.15.0", Compiler:"gc", Platform:"linux/amd64"} Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"", GitTreeState:"clean", BuildDate:"2021-01-13T13:20:00Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"} The behavior has been seen in both local minikube and OpenShift distributions based upon Kubernetes 1.20. OS (e.g: ): Fedora 33
/sig api-machinery
Is this the same issue as ?
It could be related, but in that case the client is calling close. In this case we expect the server to initiate the close.
A couple of things were discussed when we triaged this issue: a) we need to involve the Fabric8 team (~java client?) b) We don't always default to Websocket, in fact we discussed discontinuing it c) But the code is still there, so it's allowed for now and we should support it. Not sure what to do next
a) no, it sounds like an apiserver problem, it should be reproducible with a go client in a test. b) If anything, we should standardize on websockets. Brower based clients depend on them. And when/if we get rid of SPDY, websockets are the replacement. This may be relevant to interests.

I didn't see an option to use websockets with the go client the impl is at Is there another code path or go client that will use a websocket for Watches?
Technically I didn't say our go client, just a go client. I just had in mind using the raw websockets library. must be testing the websocket watches somehow?
I adapted to It will simply keep running - with no events after the timeout. If you switch to configmap, it will terminate.
that's a terrific reproducer, thanks!
Yeah, we should absolutely be able to stick that in a unit / integration test somewhere, thanks!
opened in configmap watch works correctly, service watch hits timeout without the server closing the connection
I suspect the issue is in newDecoratedWatcher. Services implement a decorator and configmaps don't.
yup, the request context wasn't propagated to the decorated watcher
Service decorator was in 1.20 in Fix with unit and integration test in Will pick to 1.21 and 1.20 once merged
/triage accepted