Skip to content

TCP port remapping fails with EADDRINUSE when virtual port is already in use #2

@ghazariann

Description

@ghazariann

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

Image

Environment: Linux 6.6, Python 3.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions