Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSError: [Errno 22] Invalid argument, whenever getting input after PIPE in #1943

Open
KoStard opened this issue Dec 13, 2024 · 2 comments
Open

Comments

@KoStard
Copy link

KoStard commented Dec 13, 2024

Hello team,

Thank you for the useful library!

I have an issue I would like to report.
The below code works if we replace the prompt_toolkit's prompt() with simple input(). I would like to have the ability to accept input after the user piped in some content. But it fails whenever using prompt toolkit in this scenario.

"""
Minimal example demonstrating prompt_toolkit issue with reopened TTY.
Run with: echo "test" | python minimal_tty_issue.py
"""
import sys
from prompt_toolkit import prompt

def main():
    # Simulate piped input scenario
    if not sys.stdin.isatty():
        content = sys.stdin.read()
        print(f"Got from pipe: {content}")
        
        # Reopen TTY - this causes the issue
        sys.stdin = open('/dev/tty', 'r')

    # Try to get interactive input
    user_input = prompt("")
    print(f"Got interactively: {user_input}")

if __name__ == "__main__":
    main()
❯ echo "test" | python minimal_tty_issue.py
Got from pipe: test

Traceback (most recent call last):
  File "/Users/kostard/.local/share/mise/installs/python/3.11/lib/python3.11/asyncio/selector_events.py", line 269, in _add_reader
    key = self._selector.get_key(fd)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kostard/.local/share/mise/installs/python/3.11/lib/python3.11/selectors.py", line 192, in get_key
    raise KeyError("{!r} is not registered".format(fileobj)) from None
KeyError: '3 is not registered'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/kostard/personal/HermesExperiments/cli/input/minimal_tty_issue.py", line 22, in <module>
    main()
  File "/Users/kostard/personal/HermesExperiments/cli/input/minimal_tty_issue.py", line 18, in main
    user_input = prompt("")
                 ^^^^^^^^^^
  File "/Users/kostard/.local/share/mise/installs/python/3.11/lib/python3.11/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1425, in prompt
    return session.prompt(
           ^^^^^^^^^^^^^^^
  File "/Users/kostard/.local/share/mise/installs/python/3.11/lib/python3.11/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1035, in prompt
    return self.app.run(
           ^^^^^^^^^^^^^
  File "/Users/kostard/.local/share/mise/installs/python/3.11/lib/python3.11/site-packages/prompt_toolkit/application/application.py", line 1002, in run
    return asyncio.run(coro)
           ^^^^^^^^^^^^^^^^^
  File "/Users/kostard/.local/share/mise/installs/python/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/Users/kostard/.local/share/mise/installs/python/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kostard/.local/share/mise/installs/python/3.11/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/kostard/.local/share/mise/installs/python/3.11/lib/python3.11/site-packages/prompt_toolkit/application/application.py", line 886, in run_async
    return await _run_async(f)
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/kostard/.local/share/mise/installs/python/3.11/lib/python3.11/site-packages/prompt_toolkit/application/application.py", line 734, in _run_async
    with self.input.raw_mode(), self.input.attach(
  File "/Users/kostard/.local/share/mise/installs/python/3.11/lib/python3.11/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/Users/kostard/.local/share/mise/installs/python/3.11/lib/python3.11/site-packages/prompt_toolkit/input/vt100.py", line 165, in _attached_input
    loop.add_reader(fd, callback_wrapper)
  File "/Users/kostard/.local/share/mise/installs/python/3.11/lib/python3.11/asyncio/selector_events.py", line 344, in add_reader
    self._add_reader(fd, callback, *args)
  File "/Users/kostard/.local/share/mise/installs/python/3.11/lib/python3.11/asyncio/selector_events.py", line 271, in _add_reader
    self._selector.register(fd, selectors.EVENT_READ,
  File "/Users/kostard/.local/share/mise/installs/python/3.11/lib/python3.11/selectors.py", line 523, in register
    self._selector.control([kev], 0, 0)
OSError: [Errno 22] Invalid argument
@ErikBjare
Copy link

ErikBjare commented Dec 15, 2024

Having the same issue in ErikBjare/gptme#244

I used to have a similar workaround which worked with rich: https://github.com/ErikBjare/gptme/blob/8e8f28ea1f7dd244be6177084a1b828646ac4ce1/gptme/cli.py#L205-L223

@ErikBjare
Copy link

I found a workaround: #502 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants