Skip to content

Suggested change to http.server.HTTPServer to prevent socket reuse in Windows #85307

Open

Description

@MichaelRich
BPO 41135

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2020-06-27.04:21:05.559>
labels = ['3.8', 'type-bug', 'library']
title = 'Suggested change to http.server.HTTPServer to prevent socket reuse in Windows'
updated_at = <Date 2020-06-27.04:21:05.559>
user = 'https://bugs.python.org/MichaelRich'

bugs.python.org fields:

activity = <Date 2020-06-27.04:21:05.559>
actor = 'Michael Rich'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2020-06-27.04:21:05.559>
creator = 'Michael Rich'
dependencies = []
files = []
hgrepos = []
issue_num = 41135
keywords = []
message_count = 1.0
messages = ['372451']
nosy_count = 1.0
nosy_names = ['Michael Rich']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue41135'
versions = ['Python 3.8']

Activity

  1. MichaelRich commented on Jun 27, 2020

    MichaelRichmannequin
    MannequinAuthor

    Hi, a web server can be incorrectly bound to an already in-use socket when binding a HTTPServer on windows. The issue is discussed here: https://stackoverflow.com/questions/51090637/running-a-python-web-server-twice-on-the-same-port-on-windows-no-port-already

    This only happens on Windows. In *nix the socketserver will throw an error, on Windows it will not. However the most recently bound server will not receive the requests.

    I suggest the following code (taken from stackoverflow) at the start of the server_bind method:

    if hasattr(socket, 'SO_EXCLUSIVEADDRUSE'):
                self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
                # Also need to change the value of allow_reuse_address (defined in http.server.HTTPServer)
                HTTPServer.allow_reuse_address = 0

    I have tested this and it will throw an error upon reuse in Windows and does not change *nix behavior.

    Thanks,

    Mike

  2. added
    3.8 (EOL)
    stdlib
    type-bug
    on Jun 27, 2020
  3. transferred this issue fromon Apr 10, 2022
  4. martingd commented on Dec 2, 2022

    @martingd

    I have the same issue, and it seems enough to set allow_reuse_address = False as HTTPServer apparantly sets it to True on Windows.

    For example, subclassing HTTPServer and overriding the server_bind() method like this fixes the issue.

    class CustomHTTPServer(HTTPServer):
        def __init__(self, server_address, RequestHandlerClass):
            super().__init__(server_address, RequestHandlerClass)
    
        def server_bind(self):
            self.allow_reuse_address = False
            super().server_bind()

    No need to set the SO_EXCLUSIVEADDRUSE option to fix this problem.

    I havent's checked if it is only on Windows the SO_REUSEADDR socket option is set by default but I don't see the same problem on macOS and Linux. My guess is that SO_REUSEADDR is set on Windows to allow rebinding in the presence of TIME_WAIT entries.

    Is there a simpler wait to set allow_reuse_address = False on HTTPServer than subclassing it?

  5. kokkonisd commented on Feb 9, 2023

    @kokkonisd

    If anyone is still struggling with this, I've traced the problem back to this commit essentially: 18865de

    I'm not sure why/if this is (still) needed, and the comment next to it isn't terribly helpful.

    I've found a simpler way to get past it but it still requires subclassing:

    class CustomHTTPServer(HTTPServer):
        allow_reuse_address = False
  6. martingd commented on Feb 9, 2023

    @martingd

    @kokkonisd Did you test that, because I found that it is necessary to set allow_reuse_address = False before server_bind() is called. Please see my solution right before your comment.

  7. kokkonisd commented on Feb 9, 2023

    @kokkonisd

    @martingd yes I did test that, running two instances of the CustomHTTPServer from my snippet on the same address + port does indeed produce an error. I'm pretty sure allow_reuse_address is set to False before server_bind() is called, because what I did overwrites the "default" self.allow_reuse_address, so server_bind() will pick up the new value.

  8. added and removed
    3.8 (EOL)
    on Feb 9, 2023
  9. arhadthedev commented on Feb 9, 2023

    @arhadthedev
    Member
    if hasattr(socket, 'SO_EXCLUSIVEADDRUSE'):
                self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
                # Also need to change the value of allow_reuse_address (defined in http.server.HTTPServer)
                HTTPServer.allow_reuse_address = 0

    @gpshead Is it sufficient to create a PR or some extra corner cases are needed to be accounted?

  10. martingd commented on Feb 9, 2023

    @martingd

    @kokkonisd Maybe I misunderstand the semantics of Python when setting the field like you do.
    My point is that the constructor of HTTPServer sets allow_reuse_address = True before calling server_bind(). So in order to prevent that, I reset it in an overridden version of server_bind() before calling super().server_bind().

    My example above was cut down. Here is what I have written in code I use internally:

    class CustomHTTPServer(BaseHTTPServer.HTTPServer):
        def __init__(self, server_address, RequestHandlerClass):
            super().__init__(server_address, RequestHandlerClass)
    
        def server_bind(self):
            # On Windows, http.server.HTTPServer sets SO_REUSEADDR which allows
            # binding multiple servers to the same address. (Probably to make the server
            # bind even in the presence of TIME_WAIT entries from closed connections.)
            # We explicitly turn off this flag before binding the listen socket to ensure we
            # don't hijack the port from another instance of our program.
            # 
            # You can read reports of using the socket option SO_EXCLUSIVEADDRUSE
            # on Windows, but it is not necessary anymore (apparently).
            # Read more here:
            # * https://github.com/python-trio/trio/issues/928
            # * https://github.com/python/cpython/issues/85307
            # * https://stackoverflow.com/questions/51090637/running-a-python-web-server-twice-on-the-same-port-on-windows-no-port-already
            self.allow_reuse_address = False
            super().server_bind()

    @arhadthedev see my comments about SO_EXCLUSIVEADDRUSE in the code above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-windowsstdlibtype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions