-
-
Notifications
You must be signed in to change notification settings - Fork 74
Mimic Dash error if running app on port already in use #79
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ | |
|
||
__author__ = "Jonas Van Der Donckt, Jeroen Van Der Donckt, Emiel Deprost" | ||
|
||
import sys | ||
import socket | ||
import warnings | ||
from typing import Tuple | ||
|
||
|
@@ -160,6 +162,15 @@ def show_dash( | |
self._host = kwargs.get("host", "127.0.0.1") | ||
self._port = kwargs.get("port", "8050") | ||
|
||
# check if self._port is already in use | ||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
if s.connect_ex(('localhost', int(self._port))) == 0: | ||
error_message = "Address already in use\n" \ | ||
f"Port {self._port} is in use by another program. " \ | ||
"Either identify and stop that program, or start the server with a different port." | ||
print(error_message) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is print + sys.exit used? Isn't it clearer to raise an error with that error message? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you are right here, but when I run a dash app twice on the same host and port I dont get an error message thrown, it just tells me the same message above and quits. Not sure if this is within flask or dash but would be good to check that against those two as well as fastapi. Definitely agree that swallowing the error and quitting isnt ideal, so maybe an upstream change could be welcome here wherever that implementation lies. Another test I need to make is if dash has this same behavior in a notebook as we are seeing here (taking too long to let the user know that the port is already in use) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the other real reason I dont want to throw an error here is that this is just calling the upstream libraries and honestly shouldnt be this library's problem to catch that imo as all it does is resample really well. I will do some digging at some point today to confirm any assumptions in the above comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll have a look myself later this week (I guess we should look at dash & jupyter dash) :) Would be amazing if this PR avoids the cases where it takes dash way too long to notify the users when the port is already used! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, just validated this PR! Seems to work as expected 🔥 My 5 cents about sys.exit vs raising an error; although throwing an error in these cases is not at all the responsibility of this library, I think calling In an ideal world we would fix the issue / weird behavior in the upstream library (but I do not seem to find quickly where these issues stem from) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I raised the error and for reference here is where it is implemented in werkzeug, which is the error we were/are seeing with this change https://github.com/pallets/werkzeug/blob/edef71c243e1e1396092f7b5f82ddad6c6f766cf/src/werkzeug/serving.py#L907 |
||
sys.exit(1) | ||
|
||
app.run_server(mode=mode, **kwargs) | ||
|
||
def stop_server(self, warn: bool = True): | ||
|
Uh oh!
There was an error while loading. Please reload this page.