python/cpython · error · ValueError
host/port and sock can not be specified at the same time
Error message
host/port and sock can not be specified at the same time
What it means
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.
Source
Thrown at Lib/asyncio/base_events.py:1122
if ssl_handshake_timeout is not None and not ssl:
raise ValueError(
'ssl_handshake_timeout is only meaningful with ssl')
if ssl_shutdown_timeout is not None and not ssl:
raise ValueError(
'ssl_shutdown_timeout is only meaningful with ssl')
if sock is not None:
_check_ssl_socket(sock)
if happy_eyeballs_delay is not None and interleave is None:
# If using happy eyeballs, default to interleave addresses by family
interleave = 1
if host is not None or port is not None:
if sock is not None:
raise ValueError(
'host/port and sock can not be specified at the same time')
infos = await self._ensure_resolved(
(host, port), family=family,
type=socket.SOCK_STREAM, proto=proto, flags=flags, loop=self)
if not infos:
raise OSError('getaddrinfo() returned empty list')
if local_addr is not None:
laddr_infos = await self._ensure_resolved(
local_addr, family=family,
type=socket.SOCK_STREAM, proto=proto,
flags=flags, loop=self)
if not laddr_infos:
raise OSError('getaddrinfo() returned empty list')
else:
laddr_infos = None
View on GitHub (pinned to bc6749cc3b)
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.
Example fix
// before
sock = socket.socket()
await loop.create_connection(proto, 'example.com', 443, sock=sock)
// after
sock.close()
transport, proto = await loop.create_connection(
proto, 'example.com', 443, local_addr=('192.0.2.5', 0)) Defensive patterns
Strategy: validation
Validate before calling
if (host is not None or port is not None) and sock is not None:
raise ConfigError('pass host/port or sock, not both') Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- A Stream Socket was expected, got {sock!r}
- server_hostname is only meaningful with ssl
- host and port was not specified and no sock specified
- Unimplemented ioctl request
- profiling_trace: Placeholder pattern not found in {js_path.n
AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14).
Data as JSON: /api/errors/a1afe85557e1e9b6.
Report an issue: GitHub.