python icon indicating copy to clipboard operation
python copied to clipboard

Not possible to close watch stream immediately

Open Lirt opened this issue 1 year ago • 7 comments

Hello Kubernetes Python Community,

I would like to ask for a help about what is the intended way to close the watch stream immediately.

Consider my simple reproducible example python3 code where I want to close the stream by sending signal:

#!/usr/bin/env python3

import signal
from kubernetes import client, config, watch

watcher_cm = watch.Watch()


def cleanup(sig, frame):
    print("Cleaning up watcher streams")
    watcher_cm.stop()
    print("Cleanup Finished")


def main():
    config.load_kube_config()
    signal.signal(signal.SIGINT, cleanup)
    signal.signal(signal.SIGTERM, cleanup)

    v1 = client.CoreV1Api()
    event: client.CoreV1Event

    try:
        for event in watcher_cm.stream(
                func=v1.list_namespaced_config_map,
                namespace="kube-system",
                label_selector="app=kube-proxy"):
            print('Registered new ConfigMap event')
    except Exception as e:
        print(e)


main()
  1. I run the script;
  2. It registers one ConfigMap;
  3. Ten I hit Ctrl+C;
  4. I expect that the watch stream will try to end as soon as possible, but it is hanging. Probably it waits for one more next() event before it closes?;
  5. While the script is hanging, in the second terminal I execute kubectl label cm kube-proxy foo=bar to create new event;
  6. The script finishes now;
$ python3 test.py
Registered new ConfigMap event
^CCleaning up watcher streams
Cleanup Finished
# Here the script is hanging...
# But when I do `kubectl label cm kube-proxy foo=bar` in the second terminal, it registers one more event and closes the watch stream.
Registered new ConfigMap event

The question is how do I exit the watch stream without waiting for one more event?

Environment:

  • Kubernetes version (kubectl version): v1.23.9
  • OS (e.g., MacOS 10.13.6): Fedora 35
  • Python version (python --version): 3.10.4
  • Python client version (pip list | grep kubernetes): kubernetes 24.2.0

Thank you very much for any help.

Lirt avatar Aug 20 '22 16:08 Lirt

I would like to contribute to solve this issue. So if possible, can you please assign this issue to me and help me with some resources for the issue? @dims @lavalamp @roycaihw @SEJeff @rawler @flavio @yuvipanda @brejoc @spiffxp @gliptak @iciclespider @larsks @jfrabaute @aschleck @idvoretskyi @AurelienGasser @tg123 @webwurst @hajowieland @mccullya @willthames @zapman449 @WilliamDenniss @unki @juliantaylor @dhague @justaugustus @mitar

sharmadhiraj86 avatar Sep 10 '22 04:09 sharmadhiraj86

Hi @sharmadhiraj86,

I cannot assign anyone as I am not maintainer of the project. Maybe @fabianvf or someone else from OWNERS file could help here?

But I'd say if you open a PR it will probably be automatically assigned to someone from owners.

Lirt avatar Sep 10 '22 11:09 Lirt

Instead of tagging every single contributor (which is very annoying), just do the work and open a pull request please.

SEJeff avatar Sep 10 '22 17:09 SEJeff

Instead of tagging every single contributor (which is very annoying), just do the work and open a pull request please.

But before that, I need this task assigned to me officially as I have to show this as a proof.

sharmadhiraj86 avatar Sep 10 '22 18:09 sharmadhiraj86

Anyone can contribute when they want. Please just send a PR.

SEJeff avatar Sep 10 '22 19:09 SEJeff

Try coding your cleanup method like this:

def cleanup(sig, frame):
    print("Cleaning up watcher streams")
    raise Exception('Exit the watcher event loop')

From socket.recv:

Changed in version 3.5: If the system call is interrupted and the signal handler does not raise an exception, the method now retries the system call instead of raising an InterruptedError exception (see PEP 475 for the rationale).

iciclespider avatar Sep 11 '22 03:09 iciclespider

Hi @iciclespider,

Thank you for your advice and useful links. It works now :pray:!

From my side this solves the issue. If you would like, I can open PR with an example to better clarify how to interrupt the watch stream correctly. If not feel free to close the issue.

Lirt avatar Sep 11 '22 09:09 Lirt