From 5e10f51bc52b09dd04b16e8830e0e920f387294e Mon Sep 17 00:00:00 2001 From: mvdbeek Date: Thu, 13 Jun 2024 19:16:32 +0200 Subject: [PATCH 1/2] Always close pycurl instance The class has a `__del__` method, but that's not always guaranteed to run in a timely fashion. --- pulsar/client/transport/curl.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/pulsar/client/transport/curl.py b/pulsar/client/transport/curl.py index 242e63ec..4ac87b1f 100644 --- a/pulsar/client/transport/curl.py +++ b/pulsar/client/transport/curl.py @@ -60,6 +60,7 @@ def execute(self, url, method=None, data=None, input_path=None, output_path=None if not output_path: return buf.getvalue() finally: + c.close() buf.close() @@ -69,13 +70,16 @@ def post_file(url, path): # wrap it in a better one. message = NO_SUCH_FILE_MESSAGE % (path, url) raise Exception(message) - c = _new_curl_object_for_url(url) - c.setopt(c.HTTPPOST, [("file", (c.FORM_FILE, path.encode('ascii')))]) - c.perform() - status_code = c.getinfo(HTTP_CODE) - if int(status_code) != 200: - message = POST_FAILED_MESSAGE % (url, status_code) - raise Exception(message) + try: + c = _new_curl_object_for_url(url) + c.setopt(c.HTTPPOST, [("file", (c.FORM_FILE, path.encode('ascii')))]) + c.perform() + status_code = c.getinfo(HTTP_CODE) + if int(status_code) != 200: + message = POST_FAILED_MESSAGE % (url, status_code) + raise Exception(message) + finally: + c.close() def get_size(url) -> int: @@ -122,6 +126,7 @@ def get_file(url, path: str): message = GET_FAILED_MESSAGE % (url, status_code) raise Exception(message) finally: + c.close() buf.close() From 0c006972c672bd51dba8e1874fe84501ad4f6a29 Mon Sep 17 00:00:00 2001 From: Marius van den Beek Date: Tue, 18 Jun 2024 10:41:40 +0200 Subject: [PATCH 2/2] Use context manager Co-authored-by: Nuwan Goonasekera <2070605+nuwang@users.noreply.github.com> --- pulsar/client/transport/curl.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pulsar/client/transport/curl.py b/pulsar/client/transport/curl.py index 4ac87b1f..5224d79d 100644 --- a/pulsar/client/transport/curl.py +++ b/pulsar/client/transport/curl.py @@ -1,3 +1,4 @@ +import contextlib import io import logging import os.path @@ -70,16 +71,13 @@ def post_file(url, path): # wrap it in a better one. message = NO_SUCH_FILE_MESSAGE % (path, url) raise Exception(message) - try: - c = _new_curl_object_for_url(url) + with contextlib.closing(_new_curl_object_for_url(url)) as c: c.setopt(c.HTTPPOST, [("file", (c.FORM_FILE, path.encode('ascii')))]) c.perform() status_code = c.getinfo(HTTP_CODE) if int(status_code) != 200: message = POST_FAILED_MESSAGE % (url, status_code) raise Exception(message) - finally: - c.close() def get_size(url) -> int: