Repository navigation
Open
Description
Problem Statement
When using Sentry with ThreadPoolExecutor, I couldn't automatically capture errors like in this issue.
I think ThreadPoolExecutor use this run method.
def run(self):
if not self.future.set_running_or_notify_cancel():
return
try:
result = self.fn(*self.args, **self.kwargs)
except BaseException as exc:
self.future.set_exception(exc)
# Break a reference cycle with the exception 'exc'
self = None
else:
self.future.set_result(result)
Solution Brainstorm
In future.thread, it catches BaseException and handles it by using future.set_exception.
Therefore, I am planning to create FutureIntegration to patch Future.set_exception so that Sentry can handle it.
like this.
def setup_once():
from concurrent.futures import Future
from concurrent.futures._base import CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED, InvalidStateError
def sentry_set_exception(self, exception):
with self._condition:
if self._state in {CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED}:
raise InvalidStateError('{}: {!r}'.format(self._state, self))
self._exception = exception
self._state = FINISHED
for waiter in self._waiters:
waiter.add_exception(self)
self._condition.notify_all()
self._invoke_callbacks()
_capture_exception()
Future.set_exception = sentry_set_exception
I've tested this process in my local environment and confirmed that it works well. Would it be okay for me to contribute in this way?
Reacted by Matthieu Rigal
Metadata
Metadata
Assignees
Projects
- StatusShow more project fieldsNo status
- StatusShow more project fieldsNo status
Activity
antonpirker commentedon Jan 3, 2024
Hey @lre12 thanks for the report and yes, you should contribute in that way! I do know now the internals of futures, but If you supply a PR I will help you bringing it over the finish line
szokeasaurusrex commentedon Jul 29, 2024
@wonkajin Are you still experiencing this issue? Do you plan to create a PR, or should we take this issue over?
wonkajin commentedon Jul 31, 2024
@szokeasaurusrex Sorry, I was busy and couldn't address this for a while. This issue is still occurring, and I'll submit a PR soon.
szokeasaurusrex commentedon Jul 31, 2024
@wonkajin, all good, we just came across this issue during a periodic review of our backlog and wanted to check in! I will leave the issue on our backlog; let us know if you need any assistance preparing the PR.