When port_remap=True and the virtual port is already bound by another process,
the sandbox child gets OSError: [Errno 98] Address already in use instead of
being transparently remapped to a free port.
To reproduce:
import socket
from sandlock.policy import Policy
from sandlock.sandbox import Sandbox
code = (
"import socket; "
"s = socket.socket(socket.AF_INET, socket.SOCK_STREAM); "
"s.bind(('127.0.0.1', 8080)); "
"print(s.getsockname()[1]); "
"s.close()"
)
holder = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
holder.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
holder.bind(("127.0.0.1", 8080))
try:
result = Sandbox(Policy(port_remap=True)).run(["python3", "-c", code])
finally:
holder.close()
Expected: sandbox succeeds and getsockname() returns 8080
Actual:
OSError: [Errno 98] Address already in use
Environment: Linux 6.6, Python 3.13