Avoid picking an unreturned port twice. (#29)
* Avoid picking an unreturned port twice.
* Update ChangeLog.md
* bump version to 1.5.2
diff --git a/ChangeLog.md b/ChangeLog.md
index 3cda728..c02f5bf 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -1,3 +1,8 @@
+## 1.5.2
+
+* Do not re-pick a known used (not-yet-returned) port when running stand alone
+ without a portserver.
+
## 1.5.1
* When not using a portserver *(you really should)*, try the `bind(0)`
diff --git a/setup.cfg b/setup.cfg
index 63c1ac4..d3ad524 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,7 +1,7 @@
# https://setuptools.readthedocs.io/en/latest/setuptools.html#configuring-setup-using-setup-cfg-files
[metadata]
name = portpicker
-version = 1.5.1b1
+version = 1.5.2
maintainer = Google LLC
maintainer_email = greg@krypto.org
license = Apache 2.0
diff --git a/src/portpicker.py b/src/portpicker.py
index fc2825b..28c1971 100644
--- a/src/portpicker.py
+++ b/src/portpicker.py
@@ -205,7 +205,8 @@
# Ask the OS for an unused port.
port = bind(0, _PROTOS[0][0], _PROTOS[0][1])
# Check if this port is unused on the other protocol.
- if port and bind(port, _PROTOS[1][0], _PROTOS[1][1]):
+ if (port and port not in _random_ports and
+ bind(port, _PROTOS[1][0], _PROTOS[1][1])):
_random_ports.add(port)
return port
@@ -213,7 +214,7 @@
rng = random.Random()
for _ in range(10):
port = int(rng.randrange(15000, 25000))
- if is_port_free(port):
+ if port not in _random_ports and is_port_free(port):
_random_ports.add(port)
return port