Skip to content

fix: Python 3.14 compatibility for deprecated asyncio APIs#3139

Open
daniel7an wants to merge 1 commit intosanic-org:mainfrom
daniel7an:fix/python-3.14-signal-finalize
Open

fix: Python 3.14 compatibility for deprecated asyncio APIs#3139
daniel7an wants to merge 1 commit intosanic-org:mainfrom
daniel7an:fix/python-3.14-signal-finalize

Conversation

@daniel7an
Copy link

Summary

Fixes #3132

Python 3.14 deprecated several asyncio APIs that Sanic uses, causing DeprecationWarnings (which become errors with -W error) and, in some code paths, RuntimeErrors due to asyncio.get_event_loop() no longer creating an implicit event loop.

Changes

sanic/server/loop.py

  • Wrap asyncio.get_event_loop_policy() and asyncio.set_event_loop_policy() calls in warnings.catch_warnings() to suppress the deprecation warnings (deprecated in 3.14, removal in 3.16). This also suppresses the cascading asyncio.AbstractEventLoopPolicy deprecation from uvloop.

sanic/mixins/startup.py

  • Replace get_event_loop() with get_running_loop() in create_server() (async method) and stop() — both run inside an event loop, so get_running_loop() is correct and doesn't trigger the Python 3.14 deprecation/error.

sanic/app.py

  • Suppress deprecation warning in the loop property fallback path that uses get_event_loop_policy().get_event_loop().

sanic/server/runners.py

  • Suppress uvloop's internal use of deprecated asyncio.iscoroutinefunction in loop.add_signal_handler() calls.

sanic/server/websockets/impl.py

  • Replace asyncio.get_event_loop() with asyncio.get_running_loop() — this code always runs within an active event loop.

Testing

  • All signal-related tests pass (test_signals.py, test_signal_handlers.py)
  • Production mode (single and multi-worker) starts and serves requests without deprecation warnings on Python 3.14.2
  • Verified with python3 -W error::DeprecationWarning — no sanic-originated deprecation warnings remain.

Fixes sanic-org#3132

- Suppress DeprecationWarning for asyncio.get_event_loop_policy() and
  asyncio.set_event_loop_policy() which are deprecated in Python 3.14
  and slated for removal in 3.16
- Replace asyncio.get_event_loop() with asyncio.get_running_loop() in
  async contexts (create_server, stop, websocket impl) since
  get_event_loop() no longer creates an implicit event loop in 3.14
- Suppress uvloop's internal use of deprecated asyncio.iscoroutinefunction
  in add_signal_handler calls
@daniel7an daniel7an requested a review from a team as a code owner February 9, 2026 19:58
@codecov
Copy link

codecov bot commented Feb 9, 2026

Codecov Report

❌ Patch coverage is 54.54545% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.800%. Comparing base (785d77f) to head (67108d3).

Files with missing lines Patch % Lines
sanic/server/loop.py 55.555% 4 Missing ⚠️
sanic/server/websockets/impl.py 0.000% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##              main     #3139       +/-   ##
=============================================
+ Coverage   87.793%   87.800%   +0.007%     
=============================================
  Files          105       105               
  Lines         8143      8148        +5     
  Branches      1290      1290               
=============================================
+ Hits          7149      7154        +5     
- Misses         687       688        +1     
+ Partials       307       306        -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Tronic
Copy link
Member

Tronic commented Feb 13, 2026

Suppression of deprecation warnings is not the right solution. Python 3.16 comes very soon.

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.

Python 3.14: Production mode crashes with "Cannot finalize signals outside of event loop"

2 participants