Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix graceful shutdown #3662

Closed
wants to merge 5 commits into from
Closed

Conversation

amitbl
Copy link

@amitbl amitbl commented Mar 23, 2019

What do these changes do?

Graceful shutdown not working properly since commit dd30b2a
When a long running request is in progress, and the server is being gracefully closed, all handlers are immediately stopped the client receive a empty response.

These changes make the server stop accepting new connections, process existing ones properly and then exit.

Are there changes in behavior for the user?

On first SIGINT/SIGTERM the server will try to gracefully close all handlers and quit.
If for some reason a handler is stuck, sending a second SIGINT/SIGTERM will force the server to quit immediately

Related issue number

#3638

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

@codecov-io
Copy link

codecov-io commented Mar 23, 2019

Codecov Report

Merging #3662 into master will decrease coverage by 0.07%.
The diff coverage is 61.11%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3662      +/-   ##
==========================================
- Coverage    97.9%   97.82%   -0.08%     
==========================================
  Files          43       43              
  Lines        8556     8566      +10     
  Branches     1376     1379       +3     
==========================================
+ Hits         8377     8380       +3     
- Misses         74       78       +4     
- Partials      105      108       +3
Impacted Files Coverage Δ
aiohttp/web.py 99.09% <100%> (-0.01%) ⬇️
aiohttp/web_runner.py 94.6% <58.82%> (-3.32%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7345ffb...734365f. Read the comment docs.

@jraby
Copy link

jraby commented May 15, 2019

@asvetlov Are there plans to merge this or something similar in the near future?

Graceful shutdowns, as documented in https://aiohttp.readthedocs.io/en/stable/web_advanced.html#aiohttp-web-graceful-shutdown, do not work on 3.5.4. (clients get an empty response as soon as the aiohttp process gets a SIGTERM or SIGINT)

@asvetlov
Copy link
Member

Hi.
I'm pretty busy now by hacking asyncio for upcoming Python 3.8.
3.8 feature freeze is scheduled on the end of May.
In two weeks I will have time for reviewing pending aiohttp pull requests.

Note to myself:
As I see aiohttp 3.5 is affected, I need to backport to 3.5 as well.

@webknjaz webknjaz closed this Jul 29, 2019
@webknjaz webknjaz reopened this Jul 29, 2019
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Jul 29, 2019
@webknjaz webknjaz closed this Jul 29, 2019
@webknjaz webknjaz reopened this Jul 29, 2019
@MerliX
Copy link

MerliX commented Oct 23, 2019

Hello @asvetlov @amitbl , graceful shutdown is still important for me, will it be fixed sometime? Thank you



class GracefulExit(SystemExit):
class ForceExit(SystemExit):
Copy link
Member

Choose a reason for hiding this comment

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

Don't rename it, it's exposed and can be used by end-users.

Copy link
Member

@webknjaz webknjaz left a comment

Choose a reason for hiding this comment

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

This needs rebasing and reverting the GracefulExit

@mjholtkamp
Copy link

I ran into this problem and came across this PR. I think the problem was indeed introduced in the mentioned commit, but specifically this line: dd30b2a#diff-8e4ccf3aaf43100064462a4df10952e5R214

That line cancels all running tasks in the event loop, so also tasks that were scheduled as background tasks. After cancelling everything, it waits for them to finish. Since runner.cleanup() is called in the finally of _run_app(), it will then proceed to call on_shutdown and on_cleanup handlers.

In my case, I tried to handle shutdown gracefully by setting a flag and letting the background task gracefully exit, but it's already cancelled because of _cancel_all_tasks().

I think this PR does not fix that particular problem.

@asvetlov
Copy link
Member

I believe the issue is fixed by #3805

@asvetlov asvetlov closed this Oct 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants