Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 9ffa48a

Browse files
authored
Make sure handler.flush() doesn't deadlock. (#1112)
1 parent afca369 commit 9ffa48a

File tree

2 files changed

+18
-1
lines changed
  • contrib/opencensus-ext-azure/opencensus/ext/azure/log_exporter
  • opencensus/common/schedule

2 files changed

+18
-1
lines changed

contrib/opencensus-ext-azure/opencensus/ext/azure/log_exporter/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ def log_record_to_envelope(self, record):
106106
raise NotImplementedError # pragma: NO COVER
107107

108108
def flush(self, timeout=None):
109+
if self._queue.is_empty():
110+
return
111+
112+
# We must check the worker thread is alive, because otherwise flush
113+
# is useless. Also, it would deadlock if no timeout is given, and the
114+
# queue isn't empty.
115+
# This is a very possible scenario during process termination, when
116+
# atexit first calls handler.close() and then logging.shutdown(),
117+
# that in turn calls handler.flush() without arguments.
118+
if not self._worker.is_alive():
119+
logger.warning("Can't flush %s, worker thread is dead. "
120+
"Any pending messages will be lost.", self)
121+
return
122+
109123
self._queue.flush(timeout=timeout)
110124

111125

opencensus/common/schedule/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ def _gets(self, count, timeout):
112112
def gets(self, count, timeout):
113113
return tuple(self._gets(count, timeout))
114114

115+
def is_empty(self):
116+
return not self._queue.qsize()
117+
115118
def flush(self, timeout=None):
116119
if self._queue.qsize() == 0:
117120
return 0
@@ -124,7 +127,7 @@ def flush(self, timeout=None):
124127
return
125128
elapsed_time = time.time() - start_time
126129
wait_time = timeout and max(timeout - elapsed_time, 0)
127-
if event.wait(timeout):
130+
if event.wait(wait_time):
128131
return time.time() - start_time # time taken to flush
129132

130133
def put(self, item, block=True, timeout=None):

0 commit comments

Comments
 (0)