Skip to content

Use sys.excepthook instead of settrace#164

Open
mendral-app[bot] wants to merge 1 commit into
mainfrom
mendral/use-excepthook-instead-of-settrace
Open

Use sys.excepthook instead of settrace#164
mendral-app[bot] wants to merge 1 commit into
mainfrom
mendral/use-excepthook-instead-of-settrace

Conversation

@mendral-app

@mendral-app mendral-app Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Replace sys.settrace/threading.settrace with sys.excepthook/threading.excepthook to only capture truly unhandled exceptions, eliminating false positive Sentry reports from handled exceptions and test environments
  • Removes the significant performance overhead of sys.settrace which is called on every Python line execution

Context

Related insight: sys.settrace captures handled exceptions as errors

The previous approach used sys.settrace which fires at throw-time for ALL exceptions, before any except clause can handle them. This caused:

  1. Handled exceptions (e.g., try: vol[key] except KeyError: ...) to be reported as errors
  2. Unit test exceptions (with MagicMock objects) to appear as production Sentry errors
  3. Unnecessary performance overhead on every line of Python execution

The new approach uses sys.excepthook and threading.excepthook which only fire for exceptions that propagate without being caught — truly unhandled errors that would crash the process/thread.

Trade-off

This will only capture unhandled exceptions (which crash the process). It will no longer capture handled exceptions that might indicate SDK bugs. However, this eliminates all false positives and the performance overhead of sys.settrace.


Note

Created by Mendral. Tag @mendral-app with feedback or questions.

sys.settrace fires on ALL exceptions at throw-time, including those that
will be caught by except clauses. This causes false positive Sentry reports
for handled exceptions (e.g., KeyError in dict access with try/except) and
test framework exceptions.

Replace with sys.excepthook and threading.excepthook which only fire for
truly unhandled exceptions that would crash the process/thread. This:
- Eliminates all false positive Sentry reports from handled exceptions
- Removes performance overhead of sys.settrace (called on every line)
- Prevents test exceptions from being reported as production errors
@mendral-app mendral-app Bot requested a review from a team June 10, 2026 19:53
@mendral-app mendral-app Bot marked this pull request as ready for review June 10, 2026 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants