Skip to content

Commit d9d9145

Browse files
Merge pull request #2799 from ilcm96/watch-stop-during-request
Honor Watch.stop() called while the watch request is in flight
2 parents 50b6723 + 46b1c43 commit d9d9145

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

‎kubernetes/watch/watch.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ def stream(self, func, *args, **kwargs):
219219
resp = func(*args, **kwargs)
220220
self._resp = resp
221221
try:
222+
# stop() may have been called while the request was in
223+
# flight. Keep this check after storing self._resp: stop()
224+
# sets _stop before reading _resp, so either this check sees
225+
# the flag or stop() sees the response and shuts it down.
226+
if self._stop:
227+
break
222228
status = getattr(resp, 'status', None)
223229
if isinstance(status, int) and not 200 <= status <= 299:
224230
self._api_client.response_deserialize(resp, {})

‎kubernetes/watch/watch_test.py‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import json
1616
import os
17+
import threading
1718
import time
1819
from types import SimpleNamespace
1920
from typing import Any, Optional
@@ -429,6 +430,68 @@ def test_watch_stream_loop(self):
429430
self.assertEqual(fake_resp.close.call_count, 2)
430431
self.assertEqual(fake_resp.release_conn.call_count, 2)
431432

433+
def test_watch_stop_during_request(self):
434+
"""stop() called while the request is in flight ends the stream."""
435+
fake_resp = Mock()
436+
fake_resp.close = Mock()
437+
fake_resp.release_conn = Mock()
438+
fake_resp.stream = Mock(
439+
return_value=['{"type": "ADDED", "object": 1}\n'])
440+
441+
w = Watch(float)
442+
443+
def get_namespaces(**kwargs):
444+
# Simulate stop() from another thread before the response
445+
# headers arrive, i.e. before the stream holds the response.
446+
stopper = threading.Thread(target=w.stop)
447+
stopper.start()
448+
stopper.join(timeout=5)
449+
return fake_resp
450+
451+
fake_api = Mock()
452+
fake_api.get_namespaces = Mock(side_effect=get_namespaces)
453+
fake_api.get_namespaces.__doc__ = ':rtype: V1NamespaceList'
454+
455+
events = list(w.stream(fake_api.get_namespaces))
456+
457+
self.assertEqual([], events)
458+
fake_api.get_namespaces.assert_called_once_with(
459+
_preload_content=False, watch=True)
460+
fake_resp.stream.assert_not_called()
461+
fake_resp.close.assert_called_once()
462+
fake_resp.release_conn.assert_called_once()
463+
464+
def test_watch_stop_during_reconnect_request(self):
465+
"""stop() during a reconnect request ends the stream unread."""
466+
first_resp = Mock()
467+
first_resp.stream = Mock(
468+
return_value=['{"type": "ADDED", "object": 1}\n'])
469+
second_resp = Mock()
470+
second_resp.stream = Mock(
471+
return_value=['{"type": "ADDED", "object": 2}\n'])
472+
473+
w = Watch(float)
474+
475+
def get_namespaces(**kwargs):
476+
if fake_api.get_namespaces.call_count == 1:
477+
return first_resp
478+
stopper = threading.Thread(target=w.stop)
479+
stopper.start()
480+
stopper.join(timeout=5)
481+
return second_resp
482+
483+
fake_api = Mock()
484+
fake_api.get_namespaces = Mock(side_effect=get_namespaces)
485+
fake_api.get_namespaces.__doc__ = ':rtype: V1NamespaceList'
486+
487+
events = list(w.stream(fake_api.get_namespaces, resource_version='1'))
488+
489+
self.assertEqual(1, len(events))
490+
self.assertEqual(2, fake_api.get_namespaces.call_count)
491+
second_resp.stream.assert_not_called()
492+
second_resp.close.assert_called_once()
493+
second_resp.release_conn.assert_called_once()
494+
432495
def test_unmarshal_with_float_object(self):
433496
w = Watch()
434497
event = w.unmarshal_event('{"type": "ADDED", "object": 1}', 'float')

0 commit comments

Comments
 (0)