Skip to content

Propagate the traced script's sys.exit code without losing trace output#87

Merged
KaykCaputo merged 2 commits into
KaykCaputo:masterfrom
AnshBajpai05:fix/sys-exit-propagation
Jul 9, 2026
Merged

Propagate the traced script's sys.exit code without losing trace output#87
KaykCaputo merged 2 commits into
KaykCaputo:masterfrom
AnshBajpai05:fix/sys-exit-propagation

Conversation

@AnshBajpai05

Copy link
Copy Markdown
Contributor

Last of the small fixes from my fork — after this one I'd like to open a Discussion about upstreaming some larger features.

Symptom: any traced script that ends with sys.exit() — which includes most CLI tools and every argparse program — silently loses all oracletrace output:

$ oracletrace mycli.py --json trace.json
$ echo $?
0        # script's real exit code also swallowed if the trace aborted mid-way
$ ls trace.json
ls: cannot access 'trace.json': No such file or directory

runpy.run_path() lets the script's SystemExit fly straight through _trace_script, so the summary, --json/--csv/--html exports, and --compare never run, and oracletrace exits with whatever code the script raised — without having done its job.

Fix: catch SystemExit around run_path, keep tracing/reporting/exports intact, and surface the script's own exit code at the very end (Python semantics preserved: sys.exit() → 0, sys.exit(7) → 7, sys.exit("msg") → message on stderr, code 1). Gate codes (--fail-on-regression) still take precedence, since that's the CI contract.

Tests: four e2e subprocess tests — code propagated, exports written after sys.exit, string-argument semantics, bare sys.exit() success.

🤖 Generated with Claude Code

AnshBajpai05 and others added 2 commits July 9, 2026 19:20
runpy.run_path() lets a traced script's SystemExit fly through
_trace_script, so for any script ending in sys.exit() — most CLI
tools, every argparse program — the summary, all exports and
--compare silently never run.

Catch SystemExit at the run boundary, finish reporting, and return
the script's own exit code at the end. Python semantics preserved:
sys.exit() -> 0, sys.exit(7) -> 7, sys.exit('msg') -> msg on
stderr with code 1. --fail-on-regression codes still win.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

@KaykCaputo KaykCaputo left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I hadn't thought about that. That's a very important point. Thanks! Feel free to continue the discussion.

@KaykCaputo KaykCaputo merged commit 98b5ca6 into KaykCaputo:master Jul 9, 2026
4 checks passed
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.

2 participants