Title: TCPServer's server_forever() shutdown immediately when calling shutdown()
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: tontinton
Priority: normal Keywords: patch

Created on 2020-06-23 19:01 by tontinton, last changed 2020-06-27 06:02 by ned.deily.

Pull Requests
URL Status Linked Edit
PR 21093 closed tontinton, 2020-06-23 20:10
PR 21094 open tontinton, 2020-06-23 20:23
Messages (6)
msg372194 - (view) Author: Tony (tontinton) * Date: 2020-06-23 19:01
Currently calling BaseServer's shutdown() function will not make serve_forever() return immediately from it's select().

I suggest adding a new function called server_shutdown() that will make serve_forever() shutdown immediately.

Then in TCPServer(BaseServer) all we need to do is call self.socket.shutdown(socket.SHUT_RDWR) in server_shutdown()'s implementation.

To test this I made a simple script:

import threading
import time
from functools import partial
from http.server import HTTPServer, SimpleHTTPRequestHandler

def serve_http(server):

def main():
    with HTTPServer(('', 8000), SimpleHTTPRequestHandler) as server:
        t = threading.Thread(target=partial(serve_http, server))


        start = time.time()
        print(f'time it took: {time.time() - start}')

if __name__ == "__main__":
msg372195 - (view) Author: Tony (tontinton) * Date: 2020-06-23 19:02
By the way I have to ask, if I want this feature to be merged (this is my first PR) should I make a PR to 3.6/3.7/3.8/3.9 and master?

Or should I create a PR to master only? 

msg372236 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2020-06-24 07:07
> By the way I have to ask, if I want this feature to be merged (this is my first PR)

Thank you for the PR and your contribution! Unless a PR is for a problem specific to a release, simply submit a PR against the master branch. If and when a core developer chooses to merge the PR, a decision will be made about whether to backport it and we have tools to do that semi-automatically. Be aware that, since we are all volunteers here, some areas in the code base do not receive a lot of attention and http.server is one of them. So don't be discouraged if your PR is not reviewed right away. A ping down the road may help.
msg372243 - (view) Author: Tony (tontinton) * Date: 2020-06-24 09:06
Just want to note that this fixes an issue in all TCPServers and not only http.server
msg372244 - (view) Author: Tony (tontinton) * Date: 2020-06-24 09:12
This still leaves the open issue of UDPServer not shutting down immediately though
msg372440 - (view) Author: Tony (tontinton) * Date: 2020-06-26 19:40
Date User Action Args
2020-06-27 06:02:58ned.deilysetnosy: - ned.deily

versions: + Python 3.10, - Python 3.7
2020-06-26 19:40:01tontintonsetmessages: + msg372440
2020-06-24 09:12:37tontintonsetmessages: + msg372244
2020-06-24 09:06:17tontintonsetmessages: + msg372243
title: BaseServer's server_forever() shutdown immediately when calling shutdown() -> TCPServer's server_forever() shutdown immediately when calling shutdown()
2020-06-24 07:07:33ned.deilysetnosy: + ned.deily
messages: + msg372236
2020-06-23 20:23:17tontintonsetpull_requests: + pull_request20260
2020-06-23 20:10:34tontintonsetkeywords: + patch
stage: patch review
pull_requests: + pull_request20259
2020-06-23 19:02:32tontintonsetmessages: + msg372195
2020-06-23 19:01:13tontintoncreate