{"record":{"id":"eb065bba6cfdbd0c","repo":"python/cpython","slug":"write-argument-must-be-bytes-like-not-type-b","errorCode":null,"errorMessage":"write() argument must be bytes-like, not {type(b).__name__}","messagePattern":"write\\(\\) argument must be bytes-like, not (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"warning","filePath":"Lib/_android_support.py","lineNumber":126,"sourceCode":"\nclass BinaryLogStream(io.RawIOBase):\n    def __init__(self, prio, tag, fileno=None):\n        self.prio = prio\n        self.tag = tag\n        self._fileno = fileno\n\n    def __repr__(self):\n        return f\"<BinaryLogStream {self.tag!r}>\"\n\n    def writable(self):\n        return True\n\n    def write(self, b):\n        if type(b) is not bytes:\n            try:\n                b = bytes(memoryview(b))\n            except TypeError:\n                raise TypeError(\n                    f\"write() argument must be bytes-like, not {type(b).__name__}\"\n                ) from None\n\n        # Writing an empty string to the stream should have no effect.\n        if b:\n            logcat.write(self.prio, self.tag, b)\n        return len(b)\n\n    # This is needed by the test suite --timeout option, which uses faulthandler.\n    def fileno(self):\n        if self._fileno is None:\n            raise io.UnsupportedOperation(\"fileno\")\n        return self._fileno\n\n\n# When a large volume of data is written to logcat at once, e.g. when a test\n# module fails in --verbose3 mode, there's a risk of overflowing logcat's own\n# buffer and losing messages. We avoid this by imposing a rate limit using the","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_android_support.py#L108-L144","documentation":"Debug-mode guard in BaseSelectorEventLoop.sock_recvfrom(): datagram receive via the loop requires a non-blocking socket, checked with gettimeout() != 0 whenever the loop is in debug mode. The check exists because the loop's readiness model assumes any recvfrom attempt can safely return EWOULDBLOCK instead of parking the whole thread.","triggerScenarios":"await loop.sock_recvfrom(sock, bufsize) with debug mode on and a blocking socket or one carrying a nonzero timeout. Typical with UDP sockets created plainly (socket.socket(AF_INET, SOCK_DGRAM)) that never had setblocking(False) called.","commonSituations":"UDP services moved from a thread-per-socket design onto an event loop; sockets adopted from other libraries (dtls wrappers, monitoring agents) with timeouts set; CI running with asyncio debug fixtures enabled.","solutions":["sock.setblocking(False) immediately after creating the datagram socket","Centralize socket creation for the loop in one factory that enforces non-blocking mode","Keep blocking-timeout sockets away from sock_* APIs; use them only with synchronous or threaded I/O"],"exampleFix":"# before\nsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\nawait loop.sock_recvfrom(sock, 2048)  # debug: ValueError\n# after\nsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\nsock.setblocking(False)\nawait loop.sock_recvfrom(sock, 2048)","handlingStrategy":"validation","validationCode":"sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\nsock.setblocking(False)               # before first await\n\nasync def reader():\n    while True:\n        data, addr = await loop.sock_recvfrom(sock, 65536)","typeGuard":null,"tryCatchPattern":"try:\n    await loop.sock_recvfrom(sock, n)\nexcept ValueError as e:\n    if 'non-blocking' in str(e):\n        sock.setblocking(False)\n        return await loop.sock_recvfrom(sock, n)\n    raise","preventionTips":["Always setblocking(False) on UDP sockets moved onto a loop","Check blocking state when adopting sockets from third-party libs","Avoid settimeout on sockets shared with the loop","Test with debug-mode loops enabled to catch this early"],"tags":["asyncio","udp","socket","debug-mode"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}