Skip to content

fix: call post_init_error within init exception handling block - #217

Open
rudraroop wants to merge 1 commit into
mainfrom
rudraroo/fix-post-init-error-exception-context
Open

fix: call post_init_error within init exception handling block#217
rudraroop wants to merge 1 commit into
mainfrom
rudraroo/fix-post-init-error-exception-context

Conversation

@rudraroop

Copy link
Copy Markdown

Restores sys.exc_info() access for tools that monkey-patch post_init_error (e.g. Sentry SDK). The init error paths are merged into a single except block that type-checks FaultException, keeping error reporting behavior identical for both paths.

Fixes #172

Description of changes:

PR #163 moved the post_init_error call in bootstrap.run() outside the except blocks. Since Python clears the handled exception once an except block exits, sys.exc_info() returns (None, None, None) by the time post_init_error runs. This broke APM tools such as the Sentry SDK, which monkey-patch post_init_error and rely on sys.exc_info() to capture init errors (see #172).

An earlier community fix (#171) was closed because it moved the call under only one of the two except clauses, which would have dropped error reporting for the FaultException path. This PR implements the alternative suggested by the maintainer in the #171 review discussion:

  • Merge the two except clauses in run() into a single except Exception block that type-checks for FaultException, so error_result is built exactly as before for both paths.
  • Call post_init_error (and sys.exit(1)) inside the except block, restoring sys.exc_info() availability while keeping error reporting behavior identical.
  • Add a code comment referencing Recent changes broke the Sentry AWS Lambda integration #172 to guard against future regressions.
  • Add two regression tests that capture sys.exc_info() inside a mocked post_init_error and assert the active exception is visible: one for the FaultException path (bad handler) and one for the generic exception path.

No behavior change otherwise: both exception types still log the unhandled-exception warning, report the error via post_init_error, and exit with code 1. on_init_complete() already calls post_init_error inside its except block, so no change was needed there.

Tested with make test: 126 tests pass, coverage 95% (gate is 90%), black --check clean.

Target (OCI, Managed Runtime, both): both

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Restores sys.exc_info() access for tools that monkey-patch
post_init_error (e.g. Sentry SDK). The init error paths are merged
into a single except block that type-checks FaultException, keeping
error reporting behavior identical for both paths.

Fixes #172

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR restores access to the active init exception (sys.exc_info()) for integrations that monkey-patch post_init_error (e.g., Sentry), by ensuring post_init_error is invoked while the init exception is still being handled in bootstrap.run().

Changes:

  • Merge the init-time except FaultException and except Exception paths into a single except Exception block that preserves prior error_result construction behavior.
  • Move post_init_error(error_result) and sys.exit(1) inside the except block so sys.exc_info() remains populated.
  • Add regression tests that assert sys.exc_info() is non-empty inside a mocked post_init_error for both FaultException and generic-exception init failures.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
awslambdaric/bootstrap.py Calls post_init_error within the init exception handler and preserves prior FaultException vs generic exception error shaping.
tests/test_bootstrap.py Adds regression tests verifying sys.exc_info() is available inside post_init_error for both init error paths.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@rudraroop
rudraroop requested review from fabisev and vip-amzn July 27, 2026 11:14
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.

Recent changes broke the Sentry AWS Lambda integration

2 participants