{"record":{"id":"a1afe85557e1e9b6","repo":"python/cpython","slug":"host-port-and-sock-can-not-be-specified-at-the-sam","errorCode":null,"errorMessage":"host/port and sock can not be specified at the same time","messagePattern":"host/port and sock can not be specified at the same time","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/asyncio/base_events.py","lineNumber":1122,"sourceCode":"\n        if ssl_handshake_timeout is not None and not ssl:\n            raise ValueError(\n                'ssl_handshake_timeout is only meaningful with ssl')\n\n        if ssl_shutdown_timeout is not None and not ssl:\n            raise ValueError(\n                'ssl_shutdown_timeout is only meaningful with ssl')\n\n        if sock is not None:\n            _check_ssl_socket(sock)\n\n        if happy_eyeballs_delay is not None and interleave is None:\n            # If using happy eyeballs, default to interleave addresses by family\n            interleave = 1\n\n        if host is not None or port is not None:\n            if sock is not None:\n                raise ValueError(\n                    'host/port and sock can not be specified at the same time')\n\n            infos = await self._ensure_resolved(\n                (host, port), family=family,\n                type=socket.SOCK_STREAM, proto=proto, flags=flags, loop=self)\n            if not infos:\n                raise OSError('getaddrinfo() returned empty list')\n\n            if local_addr is not None:\n                laddr_infos = await self._ensure_resolved(\n                    local_addr, family=family,\n                    type=socket.SOCK_STREAM, proto=proto,\n                    flags=flags, loop=self)\n                if not laddr_infos:\n                    raise OSError('getaddrinfo() returned empty list')\n            else:\n                laddr_infos = None\n","sourceCodeStart":1104,"sourceCodeEnd":1140,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/asyncio/base_events.py#L1104-L1140","documentation":"Raised by create_connection when both host/port and a pre-connected sock are supplied. asyncio connects either by resolving host+port itself or by adopting the caller's socket — doing both is contradictory (which address would it dial?), so it raises ValueError immediately.","triggerScenarios":"loop.create_connection(proto, 'example.com', 443, sock=my_socket). Happens when code builds a socket for bind/local-address control and forgets that create_connection accepts local_addr for that, or when a fallback path accidentally leaves host set.","commonSituations":"Migrating from manual socket.socket + connect code to create_connection and passing both; refactors where host/port defaults ('localhost', 80) are always applied even in sock mode.","solutions":["Pass either host/port OR sock, never both.","If you created the socket only to control the local address, close it and use local_addr= instead.","Audit call sites where host/port have non-None defaults but sock is also threaded through."],"exampleFix":"// before\nsock = socket.socket()\nawait loop.create_connection(proto, 'example.com', 443, sock=sock)\n\n// after\nsock.close()\ntransport, proto = await loop.create_connection(\n    proto, 'example.com', 443, local_addr=('192.0.2.5', 0))","handlingStrategy":"validation","validationCode":"if (host is not None or port is not None) and sock is not None:\n    raise ConfigError('pass host/port or sock, not both')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Model connection targets as a union type: either (host, port) or sock — never both in one code path.","Use local_addr= for source-address control instead of pre-binding sockets.","Audit default arguments like host='localhost' that survive into sock-mode calls."],"tags":["asyncio","network","sockets","validation"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}