Skip to content

fix: remove return from finally block to resolve Python 3.14 SyntaxWa…#759

Open
Melissajwomack wants to merge 1 commit intoDataDog:mainfrom
Melissajwomack:fix/return-in-finally-python314-syntax-warning
Open

fix: remove return from finally block to resolve Python 3.14 SyntaxWa…#759
Melissajwomack wants to merge 1 commit intoDataDog:mainfrom
Melissajwomack:fix/return-in-finally-python314-syntax-warning

Conversation

@Melissajwomack
Copy link

@Melissajwomack Melissajwomack commented Mar 26, 2026

What does this PR do?

Removes return self.blocking_response from the finally block in _LambdaDecorator.__call__ and defers it to after the try/except/finally block, eliminating the SyntaxWarning: 'return' in a 'finally' block emitted by Python 3.14 at cold start.

Motivation

Python 3.14 introduced a SyntaxWarning for return/break/continue inside finally blocks because a return in finally silently suppresses any exception actively propagating through the try/except. This warning was firing on every Lambda cold start and polluting CloudWatch logs for anyone running on the Python 3.14 runtime.

Closes #758

Testing Guidelines

  • Verified all four AppSec blocking execution paths remain correct (block before, block during via BlockingException, block after via _after, and normal/no-blocking)
  • All existing unit tests in tests/test_wrapper.py, including TestLambdaWrapperAppsecBlocking, pass without modification
  • Deployed to an AWS Lambda function running Python 3.14 — the SyntaxWarning no longer appears in CloudWatch logs at cold start

Additional Notes

The return in finally was originally needed so that when _after() sets self.blocking_response (the "block after response" AppSec path), that value could override the tentative return self.response from the try block. The fix preserves this behavior by deferring both returns to after the finally, using a simple if self.blocking_response check.

Types of Changes

  • Bug fix

Check all that apply

  • This PR's description is comprehensive
  • This PR's changes are covered by the automated tests

…rning

Python 3.14 raises a SyntaxWarning for  inside a  block.
Move the blocking_response return to after the try/except/finally in
_LambdaDecorator.__call__, preserving all AppSec blocking execution paths.

Add a regression test confirming handler exceptions still propagate when
AppSec is enabled and no blocking response is active.
@Melissajwomack Melissajwomack requested review from a team as code owners March 26, 2026 14:13
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.

SyntaxWarning: 'return' in a 'finally' block (Python 3.14)

1 participant