You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Suggested change to http.server.HTTPServer to prevent socket reuse in Windows #85307
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=Noneclosed_at=Nonecreated_at=<Date2020-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=<Date2020-06-27.04:21:05.559>user='https://bugs.python.org/MichaelRich'
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:
ifhasattr(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.
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?
@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.
@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_addressis 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.
ifhasattr(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?
@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:
classCustomHTTPServer(BaseHTTPServer.HTTPServer):
def__init__(self, server_address, RequestHandlerClass):
super().__init__(server_address, RequestHandlerClass)
defserver_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-alreadyself.allow_reuse_address=Falsesuper().server_bind()
@arhadthedev see my comments about SO_EXCLUSIVEADDRUSE in the code above.
Activity
MichaelRich commentedon Jun 27, 2020
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:
I have tested this and it will throw an error upon reuse in Windows and does not change *nix behavior.
Thanks,
Mike
martingd commentedon Dec 2, 2022
I have the same issue, and it seems enough to set
allow_reuse_address = FalseasHTTPServerapparantly sets it toTrueon Windows.For example, subclassing
HTTPServerand overriding theserver_bind()method like this fixes the issue.No need to set the
SO_EXCLUSIVEADDRUSEoption to fix this problem.I havent's checked if it is only on Windows the
SO_REUSEADDRsocket option is set by default but I don't see the same problem on macOS and Linux. My guess is thatSO_REUSEADDRis set on Windows to allow rebinding in the presence ofTIME_WAITentries.Is there a simpler wait to set
allow_reuse_address = FalseonHTTPServerthan subclassing it?kokkonisd commentedon Feb 9, 2023
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:
martingd commentedon Feb 9, 2023
@kokkonisd Did you test that, because I found that it is necessary to set
allow_reuse_address = Falsebeforeserver_bind()is called. Please see my solution right before your comment.kokkonisd commentedon Feb 9, 2023
@martingd yes I did test that, running two instances of the
CustomHTTPServerfrom my snippet on the same address + port does indeed produce an error. I'm pretty sureallow_reuse_addressis set toFalsebeforeserver_bind()is called, because what I did overwrites the "default"self.allow_reuse_address, soserver_bind()will pick up the new value.arhadthedev commentedon Feb 9, 2023
@gpshead Is it sufficient to create a PR or some extra corner cases are needed to be accounted?
martingd commentedon Feb 9, 2023
@kokkonisd Maybe I misunderstand the semantics of Python when setting the field like you do.
My point is that the constructor of
HTTPServersetsallow_reuse_address = Truebefore callingserver_bind(). So in order to prevent that, I reset it in an overridden version ofserver_bind()before callingsuper().server_bind().My example above was cut down. Here is what I have written in code I use internally:
@arhadthedev see my comments about
SO_EXCLUSIVEADDRUSEin the code above.