Skip to content

Commit b019460

Browse files
committed
fix: Trap exceptions raised by callbacks
The other part of the fix for influxdata#558 If a configured callback results in an exception we'll trap it and log the details rather than allowing it to interrupt the parent thread
1 parent 7b786d0 commit b019460

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

influxdb_client/client/write_api.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,25 @@ def _on_next(self, response: _BatchResponse):
530530
if response.exception:
531531
logger.error("The batch item wasn't processed successfully because: %s", response.exception)
532532
if self._error_callback:
533-
self._error_callback(response.data.to_key_tuple(), response.data.data, response.exception)
533+
try:
534+
self._error_callback(response.data.to_key_tuple(), response.data.data, response.exception)
535+
except Exception as e:
536+
"""
537+
Unfortunately, because callbacks are user-provided generic code, exceptions can be entirely
538+
arbitrary
539+
540+
We trap it, log that it occurred and then proceed - there's not much more that we can
541+
really do.
542+
"""
543+
logger.error("The configured error callback threw an exception: %s", e)
544+
534545
else:
535546
logger.debug("The batch item: %s was processed successfully.", response)
536547
if self._success_callback:
537-
self._success_callback(response.data.to_key_tuple(), response.data.data)
548+
try:
549+
self._success_callback(response.data.to_key_tuple(), response.data.data)
550+
except Exception as e:
551+
logger.error("The configured success callback threw an exception: %s", e)
538552

539553
@staticmethod
540554
def _on_error(ex):

0 commit comments

Comments
 (0)