{"id":"5bf0c5886b243e17","repo":"urllib3/urllib3","slug":"must-specify-at-least-one-of-read-true-write-true","errorCode":null,"errorMessage":"must specify at least one of read=True, write=True","messagePattern":"must specify at least one of read=True, write=True","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/urllib3/util/wait.py","lineNumber":40,"sourceCode":"# altogether, for high-numbered file descriptors. The point of poll() is to fix\n# that, so on Unixes, we prefer poll().\n#\n# On Windows, there is no poll() (or at least Python doesn't provide a wrapper\n# for it), but that's OK, because on Windows, select() doesn't have this\n# strange calling convention; plain select() works fine.\n#\n# So: on Windows we use select(), and everywhere else we use poll(). We also\n# fall back to select() in case poll() is somehow broken or missing.\n\n\ndef select_wait_for_socket(\n    sock: socket.socket,\n    read: bool = False,\n    write: bool = False,\n    timeout: float | None = None,\n) -> bool:\n    if not read and not write:\n        raise RuntimeError(\"must specify at least one of read=True, write=True\")\n    rcheck = []\n    wcheck = []\n    if read:\n        rcheck.append(sock)\n    if write:\n        wcheck.append(sock)\n    # When doing a non-blocking connect, most systems signal success by\n    # marking the socket writable. Windows, though, signals success by marked\n    # it as \"exceptional\". We paper over the difference by checking the write\n    # sockets for both conditions. (The stdlib selectors module does the same\n    # thing.)\n    fn = partial(select.select, rcheck, wcheck, wcheck)\n    rready, wready, xready = fn(timeout)\n    return bool(rready or wready or xready)\n\n\ndef poll_wait_for_socket(\n    sock: socket.socket,","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/urllib3/urllib3/blob/c8d039c1b743f0bf5ee136972c68350fd91d41f1/src/urllib3/util/wait.py#L22-L58","documentation":"Raised by select_wait_for_socket when both read and write are False (or omitted). The function is meaningless without at least one direction to monitor. RuntimeError is raised. This is the select(2)-based fallback used on Windows or when poll() is unavailable.","triggerScenarios":"Calling select_wait_for_socket(sock) with no read/write, calling wait_for_socket(sock) (the dispatcher) with neither flag, or a wrapper that forwards default-False values into this function.","commonSituations":"Custom socket code that wraps wait_for_socket and forwards optional kwargs without defaults, test harnesses that call the function directly to exercise the select path, or a refactor that dropped the read=True argument.","solutions":["Pass at least one of read=True or write=True when invoking the function.","Prefer the public helpers wait_for_read(sock) or wait_for_write(sock) which set the flag for you.","Add a guard in wrappers: if not read and not write: raise ValueError before forwarding.","Audit callers to ensure they forward explicit direction flags."],"exampleFix":"// before\nready = wait_for_socket(sock)  # raises on Windows/select path\n// after\nready = wait_for_read(sock, timeout=5)  # explicit direction","handlingStrategy":"validation","validationCode":"def safe_wait_select(sock, read=False, write=False, timeout=None):\n    if not read and not write:\n        raise ValueError('need read or write')\n    return __import__('urllib3.util.wait', fromlist=['select_wait_for_socket']).select_wait_for_socket(sock, read=read, write=write, timeout=timeout)","typeGuard":"def has_direction(read: bool, write: bool) -> bool:\n    return bool(read or write)","tryCatchPattern":"from urllib3.util.wait import wait_for_read, wait_for_write\n# prefer public helpers; if calling the raw fn:\ntry:\n    select_wait_for_socket(sock, read=True, timeout=t)\nexcept RuntimeError:\n    wait_for_read(sock, timeout=t)","preventionTips":["Use the public wait_for_read/wait_for_write helpers instead of the low-level functions.","Always pass read=True or write=True explicitly.","Add an assertion in wrappers that forward direction flags."],"tags":["socket","select","misuse","validation"],"analyzedSha":"c8d039c1b743f0bf5ee136972c68350fd91d41f1","analyzedAt":"2026-08-04T20:12:33.219Z","schemaVersion":2}