gradio
gradio copied to clipboard
Set SO_REUSEADDR on stocket.
I'm running https://github.com/hlky/stable-diffusion-webui on FreeBSD 13.1.
There's an issue with repeatedly getting getting socket errors:
```Port 7860 is in use. If a gradio.Blocks is running on the port, you can close() it or gradio.close_all()```
Setting SO_REUSEADDR
in networking.py:121 fixes the issue for me
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Thanks @verm. Would you be open to making a PR? Happy to review it
We'll need a simpler repro to investigate this issue. Would you be able to make one @verm? Otherwise, let's go ahead and close this issue
Closing for lack of repro
Hi,
I want to report that setting SO_REUSEADDR
actually breaks stuff for me. This is what I got when running gradio <script>.py
with gradio 3.20.1:
Launching in *reload mode* on: http://127.0.0.1:8000 (Press CTRL+C to quit)
Watching: 'd:\work\diffusion\.conda\lib\site-packages\gradio', 'D:\work\diffusion\scripts'
ERROR: [Errno 10048] error while attempting to bind on address ('127.0.0.1', 8000): only one usage of each socket address (protocol/network address/port) is normally permitted
The issue went away when I removed s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
from networking.py
I get the same issue above on port 7860 (the default). If I run python <script.py>
or run gradio <script>.py
on another port (e.g. 8000), it then works.
@wgb-10 the same issue as @haowang1013? I don't see how their issue can happen unless the socket is still actually bound. I've seen that before in other reloadble Python programs where it reloads internally and fails to attach to the old bound socket and attempts to re-attach or the new process is launched before the old one has a chance to go away fully. Both of those situations can cause this of course it could be something else as well.
@haowang1013 What OS are you on? Is it FreeBSD and can you verify the old process has quit before the new one launches? I've never had this happen to me but I will try to replicate and come up with a better fix if so.
Thanks.
@verm Yes I have the same issue as @haowang1013 ,but I'm on windows 11.
The strange thing is that there is no other process running on port 7860.
Also, running python <script>.py
on port 7860 works fine. Its just the gradio <script>.py
command that causes the error on port 7860.
@wgb-10 Oh, Windows 11. It's tricky on Windows as they have some limitations on this to stop malicious programs from taking over a socket of an existing service. It gets a little complicated because on Windows they want you to wait until all bytes to the socket are taken care of. I think with Python you need to call socket.shutdown()
instead of socket.close()
You need to use SD_SEND/SD_RECEIVE/SD_BOTH as a flag to shutdown all communication and then close the port. After that you can re-open it just using close()
won't do it. I may be recalling this wrong and I'm not even sure how to do it in Python. It probably wants the UNIX equivalents of SHUT_RD/SHUT_WR/SHUT_RDWR. So something like:
socket.shutdown(socket.SHUT_RDWR)
Once you know all the data is taken care of then you can call socket.close()
.
Doing it this way should avoid the socket issues on all platforms without needing SO_REUSEADDR
to be set.
How can i run it on a different port ? Can you please guide me