diff --git a/google/cloud/pubsub_v1/publisher/_batch/thread.py b/google/cloud/pubsub_v1/publisher/_batch/thread.py index 36dd3b946..3f9a17f74 100644 --- a/google/cloud/pubsub_v1/publisher/_batch/thread.py +++ b/google/cloud/pubsub_v1/publisher/_batch/thread.py @@ -207,9 +207,11 @@ def commit(self): def _start_commit_thread(self): """Start a new thread to actually handle the commit.""" - + # NOTE: If the thread is *not* a daemon, a memory leak exists due to a CPython issue. + # https://github.com/googleapis/python-pubsub/issues/395#issuecomment-829910303 + # https://github.com/googleapis/python-pubsub/issues/395#issuecomment-830092418 commit_thread = threading.Thread( - name="Thread-CommitBatchPublisher", target=self._commit + name="Thread-CommitBatchPublisher", target=self._commit, daemon=True ) commit_thread.start() diff --git a/google/cloud/pubsub_v1/publisher/client.py b/google/cloud/pubsub_v1/publisher/client.py index 7e6801de0..4703cc3c4 100644 --- a/google/cloud/pubsub_v1/publisher/client.py +++ b/google/cloud/pubsub_v1/publisher/client.py @@ -375,8 +375,13 @@ def _ensure_commit_timer_runs_no_lock(self): def _start_commit_thread(self): """Start a new thread to actually wait and commit the sequencers.""" + # NOTE: If the thread is *not* a daemon, a memory leak exists due to a CPython issue. + # https://github.com/googleapis/python-pubsub/issues/395#issuecomment-829910303 + # https://github.com/googleapis/python-pubsub/issues/395#issuecomment-830092418 self._commit_thread = threading.Thread( - name="Thread-PubSubBatchCommitter", target=self._wait_and_commit_sequencers + name="Thread-PubSubBatchCommitter", + target=self._wait_and_commit_sequencers, + daemon=True, ) self._commit_thread.start()