Skip to content

Commit 98167c0

Browse files
committed
Code review feedback
1 parent 6cd7f6f commit 98167c0

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

shiny/_launchbrowser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ def emit(self, record: logging.LogRecord) -> None:
2727
self._launched = True
2828
port = os.environ["SHINY_PORT"]
2929
if not port.isnumeric():
30+
print(
31+
"SHINY_PORT environment variable not set or unusable; "
32+
"--launch-browser will be ignored"
33+
)
3034
# For some reason the shiny port isn't set correctly!?
3135
return
3236
host = os.environ["SHINY_HOST"]

shiny/_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,26 @@ def guess_mime_type(
8585
def random_port(
8686
min: int = 1024, max: int = 49151, host: str = "127.0.0.1", n: int = 20
8787
) -> int:
88+
"""Find an open TCP port
89+
90+
Finds a random available TCP port for listening on, within a specified range
91+
of ports. The default range of ports to check is 1024 to 49151, which is the
92+
set of TCP User Ports. This function automatically excludes some ports which
93+
are considered unsafe by web browsers.
94+
95+
Parameters
96+
----------
97+
min
98+
Minimum port number.
99+
max
100+
Maximum port number, inclusive.
101+
host
102+
Before returning a port number, ensure that we can successfully bind it on this
103+
host.
104+
n
105+
Number of times to attempt before giving up.
106+
"""
107+
88108
# fmt: off
89109
# From https://github.com/rstudio/httpuv/blob/main/R/random_port.R
90110
unsafe_ports = [1, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 25, 37, 42, 43, 53, 77, 79, 87, 95, 101, 102, 103, 104, 109, 110, 111, 113, 115, 117, 119, 123, 135, 139, 143, 179, 389, 427, 465, 512, 513, 514, 515, 526, 530, 531, 532, 540, 548, 556, 563, 587, 601, 636, 993, 995, 2049, 3659, 4045, 6000, 6665, 6666, 6667, 6668, 6669, 6697]

tests/test_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ def test_random_port():
140140
assert random_port(9000, 9000) == 9000
141141

142142
seen: Set[int] = set()
143+
# Ensure that 10 unique random ports are eventually generated. If not (e.g. if the
144+
# max port number is treated as exclusive instead of inclusive, say) then the while
145+
# loop will not exit and the test will timeout.
143146
while len(seen) < 10:
144147
seen.add(random_port(9001, 9010))
145148

0 commit comments

Comments
 (0)