Skip to content

Python 3.14 SyntaxWarnings: 'return' in a 'finally' block #2530

@rsolanodev

Description

@rsolanodev

Description

When running with Python 3.14, elastic-apm emits multiple SyntaxError messages about return statements inside finally blocks:

File "/usr/local/lib/python3.14/site-packages/elasticapm/contrib/django/middleware/__init__.py", line 114
    return response
    ^^^^^^^^^^^^^^^
SyntaxError: 'return' in a 'finally' block

Why This Matters

Using return inside a finally block is problematic because:

  1. It silently swallows any exceptions that were raised in the try block
  2. It masks the original exception, making debugging difficult
  3. Python 3.14 now explicitly warns about this pattern

Affected Files

elasticapm/contrib/django/middleware/__init__.py (line 114 and 129)

Proposed Fix

Refactor the code to move the return statements outside of the finally blocks while preserving the intended behavior.

Environment

  • Python version: 3.14
  • eslastic-apm version: latest (main branch)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions