fix(cli): fall back to SelectSelector when kqueue can't watch stdin on macOS#26077
Merged
Conversation
On macOS with uv-managed cPython 3.11, the default kqueue selector cannot
register fd 0, so prompt_toolkit's loop.add_reader raises
OSError(EINVAL) ("[Errno 22] Invalid argument") from kqueue.control()
and the agent crashes immediately on startup (#5884, also reported in
#6393).
Probe KqueueSelector.register(0, EVENT_READ) before launching
prompt_toolkit. If it fails, install an event-loop policy that returns a
SelectorEventLoop backed by SelectSelector — select() works fine on
stdin in this Python build, so add_reader succeeds and the agent
launches normally.
Also extend the existing #6393 fallback handler to recognize EINVAL /
EBADF / "Invalid argument" so that any future selector failure on stdin
shows the friendly "reinstall Python via pyenv or Homebrew" guidance
instead of an opaque traceback.
Verified on macOS (Darwin 24.6.0) with uv-managed cPython 3.11.15: the
kqueue probe fails, the policy switch fires, and `hermes` launches
cleanly. No effect on platforms where kqueue can register fd 0.
This was referenced May 15, 2026
Contributor
🔎 Lint report:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Salvage of #20948 by @outdoorsea onto current main.
Summary
Prevents the
OSError: [Errno 22] Invalid argumentstartup crash that hits uv-managed cPython 3.11 on macOS (issue #5884). ProbesKqueueSelector.register(0, EVENT_READ)at CLI startup; on failure, installs an event-loop policy that returns aSelectorEventLoop(SelectSelector())so prompt_toolkit'sadd_reader(0, …)works. Also widens the existingexcept (KeyError, OSError)net to matchEINVAL/EBADF/ "Invalid argument" so any future leak hits the friendly "reinstall Python via pyenv/Homebrew" message instead of a raw traceback.Changes
cli.py: kqueue probe + SelectSelector fallback beforeapp.run(); widened post-app.run()except. Gated onsys.platform == 'darwin', no-op on Linux/Windows and on Homebrew/pyenv/system Python.scripts/release.py: AUTHOR_MAP entry forjeremy@geocaching.com→outdoorsea.Validation
py_compile cli.pyok.#6393os.fstat(0)stdin pre-check.Fan-out
~15 sibling PRs (#19501, #21660, #20482, #18363, #11253, #12111, #11866, #8796, #13251, #9996, #15720, #8843, #15706, etc.) target the same crash but only widen the except. This PR is the only one that actually prevents the failure. They'll be closed pointing here.
Closes #5884
Supersedes #20482, #21660, #19501, #18363, #11253, #12111, #11866, #8796, #13251, #9996, #15720, #8843, #15706