Skip to content

Commit 38f2d74

Browse files
committed
producer/kafka: Disable logging during object destruction
Logging in an object destructor leads to crashes like the following ``` Exception ignored in: <bound method KafkaProducer.__del__ of <kafka.producer.kafka.KafkaProducer object at 0x7f0c47309b38>> Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/kafka/producer/kafka.py", line 447, in __del__ File "/usr/local/lib/python3.6/site-packages/kafka/producer/kafka.py", line 460, in close File "/usr/local/lib/python3.6/logging/__init__.py", line 1305, in info File "/usr/local/lib/python3.6/logging/__init__.py", line 1546, in isEnabledFor TypeError: '>=' not supported between instances of 'int' and 'NoneType' ``` In issue dpkp#1218 and dpkp#1445, @dpkp made clear that producers should be closed during while they are destructed. This commit does not modify the existing architecture and code structure, and preserves all the logging calls in `close`. At the same time, by "nullifying" the logger, it fixes the crash that is inherent to the use of logging methods during the destruction of an object.
1 parent f9e0264 commit 38f2d74

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

kafka/producer/kafka.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,14 @@ def _unregister_cleanup(self):
445445
self._cleanup = None
446446

447447
def __del__(self):
448+
# Disable logger during destruction to avoid touching dangling references
449+
class NullLogger(object):
450+
def __getattr__(self, name):
451+
return lambda *args: None
452+
453+
global log
454+
log = NullLogger()
455+
448456
self.close()
449457

450458
def close(self, timeout=None):

0 commit comments

Comments
 (0)