kubernetes-client / c

Official C client library for Kubernetes
Apache License 2.0
146 stars 46 forks source link

How to stop watching? #38

Closed clearday4 closed 3 years ago

clearday4 commented 3 years ago

Hi, What should I do if I want to stop watching? If I set to CURLOPT_TIMEOUT_MS, applications will receive the entire object state once again whenever they call watch after timeout. This is not WATCH but the same as calling GET repeatedly. If I kill the watch thread with pthread_cancel(), we will have the problem of leaking resources. We leak curl handle, curl mime, apiClient, cJSON_Parse in wu_convert_to_json_array() and so on. How can I gracefully exit on curl_easy_perform()? How can I stop the watch without resource leak? Please consider about it. Thanks in advance.

ityuhui commented 3 years ago

I think the best way is setting a timetout in the parameter of watch function, after timeouts, free up the resource same with non-watch example.

e.g.

    CoreV1API_listNamespacedPod(apiClient, "default",   /*namespace */
                                NULL,   /* pretty */
                                0,  /* allowWatchBookmarks */
                                NULL,   /* continue */
                                NULL,   /* fieldSelector */
                                NULL,   /* labelSelector */
                                0,  /* limit */
                                NULL,   /* resourceVersion */
                                30,  /* timeoutSeconds */     <------------------ Set the timeout
                                1   /* watch */
        );
clearday4 commented 3 years ago

I think the best way is setting a timetout in the parameter of watch function, after timeouts, free up the resource same with non-watch example.

e.g.

    CoreV1API_listNamespacedPod(apiClient, "default",   /*namespace */
                                NULL,   /* pretty */
                                0,  /* allowWatchBookmarks */
                                NULL,   /* continue */
                                NULL,   /* fieldSelector */
                                NULL,   /* labelSelector */
                                0,  /* limit */
                                NULL,   /* resourceVersion */
                                30,  /* timeoutSeconds */     <------------------ Set the timeout
                                1   /* watch */
        );

Application want to watch the events util they stop watching. They want to receive the state of the only changed object. But if we set the timeout, application should watch again and get the entire status of object again. what is the difference from get function?

ityuhui commented 3 years ago

When timeout is set, watch function will not return even though it receives the state change.

The watch function returns only the timeout is reached.

clearday4 commented 3 years ago

It doesn't meet our requirement. application wants to know immediately if pod is scaled in or out or deleted or modified.

ityuhui commented 3 years ago

Suppose setting timeout as 3600 seconds (1 hour), Watch function can receive events such as pod deletion/modification (your callback function will be called) immediately but Watch function will not stop watching. After 3600 seconds, the watch function will return and stop watching.

clearday4 commented 3 years ago

But After 1 hour, if application call watch function again, K8s API server send the all pod state again, Therefore, if application wants to know the pod status has changed, compare to previous status and the previous information should be stored to db.

There is no different from GET that we need to store the previous state.

ityuhui commented 3 years ago

I think you can set a longer timeout. Or run watch function forever in a thread. After the program ends, allocated memory will be released.

And I see you have tried pthread_cancel(), then when will you call pthread_cancel() ?

clearday4 commented 3 years ago

If I kill the watch thread with pthread_cancel, resource leak occurs. Since watch thread is blocked on curl_easy_perform, curl handle, curl mime, apiClient and cJSON_Parse are leak

ityuhui commented 3 years ago

Could you please run watch function forever in a thread ? After the program ends, allocated memory will be released.

clearday4 commented 3 years ago

right. so application can't stop watching until now.

brendandburns commented 3 years ago

Can't you use pthread_cleanup_push to register a cleanup handler for the watch pthread?

https://man7.org/linux/man-pages/man3/pthread_cleanup_push.3.html

brendandburns commented 3 years ago

I'm closing this since I think that the issue issue is resolved via the Libcurl progress functions.

brendandburns commented 3 years ago

Please use /reopen if there is further assistance needed.