Use sys.excepthook instead of settrace#164
Open
mendral-app[bot] wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
sys.settrace/threading.settracewithsys.excepthook/threading.excepthookto only capture truly unhandled exceptions, eliminating false positive Sentry reports from handled exceptions and test environmentssys.settracewhich is called on every Python line executionContext
Related insight: sys.settrace captures handled exceptions as errors
The previous approach used
sys.settracewhich fires at throw-time for ALL exceptions, before anyexceptclause can handle them. This caused:try: vol[key] except KeyError: ...) to be reported as errorsThe new approach uses
sys.excepthookandthreading.excepthookwhich 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.