python/cpython · error · ValueError
the socket must be non-blocking
Error message
the socket must be non-blocking
What it means
Error "the socket must be non-blocking" thrown in python/cpython.
Source
Thrown at Lib/asyncio/selector_events.py:381
"""Add a writer callback.."""
self._ensure_fd_no_transport(fd)
self._add_writer(fd, callback, *args)
def remove_writer(self, fd):
"""Remove a writer callback."""
self._ensure_fd_no_transport(fd)
return self._remove_writer(fd)
async def sock_recv(self, sock, n):
"""Receive data from the socket.
The return value is a bytes object representing the data received.
The maximum amount of data to be received at once is specified by
nbytes.
"""
base_events._check_ssl_socket(sock)
if self._debug and sock.gettimeout() != 0:
raise ValueError("the socket must be non-blocking")
try:
return sock.recv(n)
except (BlockingIOError, InterruptedError):
pass
fut = self.create_future()
fd = sock.fileno()
self._ensure_fd_no_transport(fd)
handle = self._add_reader(fd, self._sock_recv, fut, sock, n)
fut.add_done_callback(
functools.partial(self._sock_read_done, fd, handle=handle))
return await fut
def _sock_read_done(self, fd, fut, handle=None):
if handle is None or not handle.cancelled():
self.remove_reader(fd)
def _sock_recv(self, fut, sock, n):
# _sock_recv() can add itself as an I/O callback if the operation can'tView on GitHub (pinned to bc6749cc3b)
Solutions
- Call sock.setblocking(False) before registering the socket with the event loop, or let asyncio create the socket for you.
When it happens
Trigger: Raised when a blocking-mode socket is used with the selector event loop.
Common situations: See trigger scenarios.
AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14).
Data as JSON: /api/errors/c667eeaf6f307655.
Report an issue: GitHub.