From 09a6b588fe7df63c6cd8acb6699cab46f8f57bec Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Wed, 1 Sep 2021 19:31:18 -0700 Subject: [PATCH] [Python] Ensure producer is keeping the client object alive (#11887) ### Motivation Fix #6463. When a producer is created, keep a reference on the client object so that the Python GC will not destroy it. This is similar to what we were already doing for consumer and reader, were we had examples with listeners that need to keep the client/consumer alive. (cherry picked from commit 235e678a56d0284e68b45e46706b6237d7c6d5f9) --- pulsar-client-cpp/python/pulsar/__init__.py | 1 + pulsar-client-cpp/python/pulsar_test.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/pulsar-client-cpp/python/pulsar/__init__.py b/pulsar-client-cpp/python/pulsar/__init__.py index 514ca11dc7d0d..429aa1040e12f 100644 --- a/pulsar-client-cpp/python/pulsar/__init__.py +++ b/pulsar-client-cpp/python/pulsar/__init__.py @@ -588,6 +588,7 @@ def create_producer(self, topic, p = Producer() p._producer = self._client.create_producer(topic, conf) p._schema = schema + p._client = self._client return p def subscribe(self, topic, subscription_name, diff --git a/pulsar-client-cpp/python/pulsar_test.py b/pulsar-client-cpp/python/pulsar_test.py index 58107456f44a0..4a8ca039f6b0b 100755 --- a/pulsar-client-cpp/python/pulsar_test.py +++ b/pulsar-client-cpp/python/pulsar_test.py @@ -1096,6 +1096,14 @@ def test_producer_consumer_zstd(self): consumer.unsubscribe() client.close() + def test_client_reference_deleted(self): + def get_producer(): + cl = Client(self.serviceUrl) + return cl.create_producer(topic='foobar') + + producer = get_producer() + producer.send(b'test_payload') + ##### def test_get_topic_name(self):