{"record":{"id":"6fdd4f8bd5b4438d","repo":"python/cpython","slug":"reuse-port-not-supported-by-socket-module","errorCode":null,"errorMessage":"reuse_port not supported by socket module","messagePattern":"reuse_port not supported by socket module","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/asyncio/base_events.py","lineNumber":94,"sourceCode":"    if isinstance(getattr(cb, '__self__', None), tasks.Task):\n        # format the task\n        return repr(cb.__self__)\n    else:\n        return str(handle)\n\n\ndef _format_pipe(fd):\n    if fd == subprocess.PIPE:\n        return '<pipe>'\n    elif fd == subprocess.STDOUT:\n        return '<stdout>'\n    else:\n        return repr(fd)\n\n\ndef _set_reuseport(sock):\n    if not hasattr(socket, 'SO_REUSEPORT'):\n        raise ValueError('reuse_port not supported by socket module')\n    else:\n        try:\n            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)\n        except OSError:\n            raise ValueError('reuse_port not supported by socket module, '\n                             'SO_REUSEPORT defined but not implemented.')\n\n\ndef _ipaddr_info(host, port, family, type, proto, flowinfo=0, scopeid=0):\n    # Try to skip getaddrinfo if \"host\" is already an IP. Users might have\n    # handled name resolution in their own code and pass in resolved IPs.\n    if not hasattr(socket, 'inet_pton'):\n        return\n\n    if proto not in {0, socket.IPPROTO_TCP, socket.IPPROTO_UDP} or \\\n            host is None:\n        return None\n","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/asyncio/base_events.py#L76-L112","documentation":"asyncio's _set_reuseport helper is invoked when reuse_port=True is passed to loop.create_server (or similar socket-creating APIs). It first checks whether the socket module even exposes SO_REUSEPORT; on platforms without it (notably Windows) it raises ValueError immediately, telling the caller the feature cannot be requested at all.","triggerScenarios":"loop.create_server(handler, host, port, reuse_port=True) on Windows or any platform whose socket module lacks SO_REUSEPORT; also when a custom/limited socket shim (e.g. restricted embedded builds) hides the constant.","commonSituations":"Code developed on Linux (where SO_REUSEPORT enables multi-process port sharing) deployed to Windows services; containerized builds using minimal libc where the constant is compiled out; config files that unconditionally enable reuse_port across a heterogeneous fleet.","solutions":["Pass reuse_port=False (or omit it) on platforms without SO_REUSEPORT support.","Feature-detect before calling: use reuse_port=hasattr(socket, 'SO_REUSEPORT') and your platform policy.","On Windows, drop the multi-listener pattern that SO_REUSEPORT enables; use a single accepting process or an external load balancer."],"exampleFix":"# before\nserver = await loop.create_server(handler, '0.0.0.0', 8080, reuse_port=True)\n# on Windows -> ValueError: reuse_port not supported by socket module\n\n# after\nreuse = hasattr(socket, 'SO_REUSEPORT') and sys.platform != 'win32'\nserver = await loop.create_server(handler, '0.0.0.0', 8080, reuse_port=reuse)","handlingStrategy":"validation","validationCode":"import socket\n\ndef reuseport_available() -> bool:\n    return hasattr(socket, 'SO_REUSEPORT')","typeGuard":null,"tryCatchPattern":"try:\n    server = await loop.create_server(h, host, port, reuse_port=True)\nexcept ValueError as e:\n    if 'reuse_port not supported' in str(e):\n        server = await loop.create_server(h, host, port)  # retry without it","preventionTips":["Never hardcode reuse_port=True in code that must run on Windows.","Feature-detect with hasattr(socket, 'SO_REUSEPORT') and thread the boolean through config.","Surface a startup warning when the requested reuse_port had to be disabled."],"tags":["asyncio","sockets","so-reuseport","platform-support"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}