Skip to content

Reset how loop grabbed in shutdown#3123

Open
ahopkins wants to merge 3 commits intomainfrom
issue2906
Open

Reset how loop grabbed in shutdown#3123
ahopkins wants to merge 3 commits intomainfrom
issue2906

Conversation

@ahopkins
Copy link
Member

Closes #2906

shutdown_tasks() was using get_running_loop() which fails when called outside of a running coroutine, causing the sleep to be silently skipped, creating an infinite loop.

Copilot AI review requested due to automatic review settings December 31, 2025 13:27
@ahopkins ahopkins requested a review from a team as a code owner December 31, 2025 13:27
Copy link
Contributor

Copilot AI left a comment

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 fixes an infinite loop issue in the shutdown process by ensuring the event loop is properly passed to shutdown_tasks(). The problem occurred because get_running_loop() was being called from outside a running coroutine context, causing it to fail silently and skip the sleep operation.

Key Changes:

  • Pass the event loop explicitly from _cleanup() in runners.py to shutdown_tasks()
  • Add optional loop parameter to shutdown_tasks() with fallback logic to obtain the loop if not provided
  • Improve the while loop condition to check timeout > 0 instead of just truthiness

Reviewed changes

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

File Description
sanic/server/runners.py Passes the event loop to shutdown_tasks() call during cleanup to ensure proper loop availability
sanic/app.py Adds optional loop parameter to shutdown_tasks() method with fallback logic and refactors loop acquisition and exception handling

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

@codecov
Copy link

codecov bot commented Dec 31, 2025

Codecov Report

❌ Patch coverage is 63.63636% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.789%. Comparing base (785d77f) to head (d8fd598).

Files with missing lines Patch % Lines
sanic/app.py 60.000% 4 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##              main     #3123       +/-   ##
=============================================
- Coverage   87.793%   87.789%   -0.004%     
=============================================
  Files          105       105               
  Lines         8143      8149        +6     
  Branches      1290      1291        +1     
=============================================
+ Hits          7149      7154        +5     
- Misses         687       689        +2     
+ 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.

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.

Shutdown never finishes when background tasks are cancelled but need some time to finish

2 participants