fix(startup): Differentiate between None vs. 0 port#3037
Open
DarwinsBuddy wants to merge 1 commit intosanic-org:mainfrom
Open
fix(startup): Differentiate between None vs. 0 port#3037DarwinsBuddy wants to merge 1 commit intosanic-org:mainfrom
DarwinsBuddy wants to merge 1 commit intosanic-org:mainfrom
Conversation
8e08f59 to
29a0827
Compare
Author
|
Unfortunately, I cannot comply to the CONTRIBUTING guidelines, as the parent commit already conflicts with it. If I can do more to kick the can please let me know. |
ahopkins
reviewed
Mar 5, 2025
| """ # noqa: E501 | ||
| host = host or "127.0.0.1" | ||
| port = port or (8443 if (version == 3 or auto_tls) else 8000) | ||
| if port is None: |
Member
There was a problem hiding this comment.
I am generally fine with this approach, but I think maybe we should be explicit.
if port is None:
# use a default
elif port is 0:
port = _get_random_port()
...In this case we would bind to get something from the os, but immediately close. I suppose in theory this might fail for some interval, would need to explore that. But I think we want to ensure that get_address does indeed return tuple[str, int]. If not I suspect the changes might need to be deeper here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I'm currently developing a sanic application which should support service discovery.
In order to have a degree of freedom in choosing how to run sanic in this context, I'm in need of random ports to be chosen by the operating system.
Intentionally, at least according to the docs, this should be possible by simply setting port either to
Noneor0.Though, I stumbled upon the
mixin/startupcode which is in case of a tls start up fixing this to8443or8000in both occasions, since coalescing port like thisport = port or 8443 if (version == 3 or auto_tls) else 8000will be in both cases (
0andNone) fallback to this term8443 if (version == 3 or auto_tls) else 8000Since I do not want to break anything, I suggest changing this to the following behaviour:
In case of
port=Nonepersist the logic as isIn case of
port=0let the OS handle choosing a port for us (which would be a random port)Feedback is very much welcome and I'm open for alternative approaches.
If we could enable this one way or the other I'd very much appreciate it.
(Thank you for this awesome framework ❤️ )