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

Error Bad file descriptor in prompt_toolkit > 3.0.40 #5241

Open
doronz88 opened this issue Dec 3, 2023 · 20 comments
Open

Error Bad file descriptor in prompt_toolkit > 3.0.40 #5241

doronz88 opened this issue Dec 3, 2023 · 20 comments

Comments

@doronz88
Copy link
Contributor

doronz88 commented Dec 3, 2023

Testing environment: macOS Sonoma 14.1.2

After a git checkout, or directory change I very randomly get the following error:

Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/xonsh/main.py", line 469, in main
    sys.exit(main_xonsh(args))
             ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/xonsh/main.py", line 513, in main_xonsh
    shell.shell.cmdloop()
  File "/opt/homebrew/lib/python3.11/site-packages/xonsh/ptk_shell/shell.py", line 401, in cmdloop
    line = self.singleline(auto_suggest=auto_suggest)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/xonsh/ptk_shell/shell.py", line 369, in singleline
    line = self.prompter.prompt(**prompt_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1026, in prompt
    return self.app.run(
           ^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/prompt_toolkit/application/application.py", line 998, in run
    return asyncio.run(coro)
           ^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 189, in run
    with Runner(debug=debug) as runner:
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 63, in __exit__
    self.close()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 77, in close
    loop.close()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/unix_events.py", line 68, in close
    super().close()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/selector_events.py", line 91, in close
    self._close_self_pipe()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/selector_events.py", line 99, in _close_self_pipe
    self._ssock.close()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/socket.py", line 503, in close
    self._real_close()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/socket.py", line 497, in _real_close
    _ss.close(self)
OSError: [Errno 9] Bad file descriptor
Xonsh encountered an issue during launch
Failback to /bin/bash

The default interactive shell is now zsh.

To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
bash-3.2$

Don't know how to force reproduction of this bug since it occurs very randomly at different times (but pretty often)

@anki-code
Copy link
Member

Just to add link to #4224

@anki-code anki-code changed the title Getting OSError: [Errno 9] Bad file descriptor at pretty random operations Getting Bad file descriptor in prompt_toolkit Feb 23, 2024
@anki-code
Copy link
Member

anki-code commented Feb 23, 2024

I can reproduce this on Mac in case of alias with group of commands:

pip install 'xonsh[full]'
xonsh --no-rc
aliases['ttt'] = 'echo 1 && echo 2'
ttt
# 1
# 2
# <Press enter>
# <Press enter>
# Exception:
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 73, in close
    loop.run_until_complete(loop.shutdown_default_executor())
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 640, in run_until_complete
    self.run_forever()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 607, in run_forever
    self._run_once()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 1884, in _run_once
    event_list = self._selector.select(timeout)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/selectors.py", line 566, in select
    kev_list = self._selector.control(None, max_ev, timeout)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/xonsh/main.py", line 469, in main
    sys.exit(main_xonsh(args))
             ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/xonsh/main.py", line 513, in main_xonsh
    shell.shell.cmdloop()
  File "/opt/homebrew/lib/python3.11/site-packages/xonsh/ptk_shell/shell.py", line 405, in cmdloop
    line = self.singleline(auto_suggest=auto_suggest)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/xonsh/ptk_shell/shell.py", line 373, in singleline
    line = self.prompter.prompt(**prompt_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1026, in prompt
    return self.app.run(
           ^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/prompt_toolkit/application/application.py", line 1002, in run
    return asyncio.run(coro)
           ^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 189, in run
    with Runner(debug=debug) as runner:
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 63, in __exit__
    self.close()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 77, in close
    loop.close()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/unix_events.py", line 68, in close
    super().close()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/selector_events.py", line 91, in close
    self._close_self_pipe()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/selector_events.py", line 99, in _close_self_pipe
    self._ssock.close()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/socket.py", line 503, in close
    self._real_close()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/socket.py", line 497, in _real_close
    _ss.close(self)
OSError: [Errno 9] Bad file descriptor
Xonsh encountered an issue during launch
Failback to /bin/zsh

@anki-code anki-code changed the title Getting Bad file descriptor in prompt_toolkit Error Bad file descriptor in prompt_toolkit Feb 23, 2024
@anki-code
Copy link
Member

anki-code commented Feb 23, 2024

I found that issue appeared starting from prompt-toolkit 3.0.41 where asyncio was introduced (prompt-toolkit/python-prompt-toolkit@8c60193).

To fix this just downgrade the prompt-toolkit version:

xpip uninstall -y prompt-toolkit
xpip install prompt-toolkit==3.0.40

The issue was reported to upstream - prompt-toolkit/python-prompt-toolkit#1853

@anki-code
Copy link
Member

anki-code commented Feb 23, 2024

@gforsyth hey! Please see my comment above. It looks like this issue is pretty critical because creates unstable behavior e.g. I see this report in news.ycombinator.com. May be we need to pin prompt-toolkit to <=3.0.40 until the issue with asyncio will be fixed. What do you think?

@doronz88
Copy link
Contributor Author

I believe this is the only way for now, though popular packages such as IPython do require newer prompt-toolkit. I guess the better choice is to add this requirement for the meantime and encourage people to install xonsh with pipx.

@anki-code anki-code changed the title Error Bad file descriptor in prompt_toolkit Error Bad file descriptor in prompt_toolkit>3.0.40 Feb 24, 2024
@anki-code anki-code changed the title Error Bad file descriptor in prompt_toolkit>3.0.40 Error Bad file descriptor in prompt_toolkit > 3.0.40 Feb 24, 2024
@gforsyth
Copy link
Collaborator

I'll be at a computer later and can take a look. In the mean time, if someone can PR the upper pin and add a note to the docs that'll make it faster to release.

@anki-code
Copy link
Member

@gforsyth here it is - #5288

@gforsyth
Copy link
Collaborator

Thanks @anki-code -- merged and released 0.15.0. Going to leave this issue open until we sort out the upstream issue or figure out what part of xonsh is tripping up prompt toolkit

@doronz88
Copy link
Contributor Author

@gforsyth @anki-code this bug persists even after this fix - but much less
now usually the "Bad File Descriptor" exception doesn't mess with the whole shell

@anki-code
Copy link
Member

anki-code commented Feb 27, 2024

I confirm this. The simple cases like alias for echo 1 && echo 2 are working on downgraded ptk but the error appears sometimes but without crashing the entire shell.

I'm also facing with this during typing the command:

 File "/opt/homebrew/lib/python3.11/site-packages/xonsh/main.py", line 470, in main
    sys.exit(main_xonsh(args))
             ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/xonsh/main.py", line 514, in main_xonsh
    shell.shell.cmdloop()
  File "/opt/homebrew/lib/python3.11/site-packages/xonsh/ptk_shell/shell.py", line 406, in cmdloop
    line = self.singleline(auto_suggest=auto_suggest)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/xonsh/ptk_shell/shell.py", line 374, in singleline
    line = self.prompter.prompt(**prompt_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1024, in prompt
    return self.app.run(
           ^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/prompt_toolkit/application/application.py", line 973, in run
    return loop.run_until_complete(coro)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 640, in run_until_complete
    self.run_forever()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 607, in run_forever
    self._run_once()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 1884, in _run_once
    event_list = self._selector.select(timeout)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/selectors.py", line 566, in select
    kev_list = self._selector.control(None, max_ev, timeout)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 9] Bad file descriptor

FlyingWombat added a commit to FlyingWombat/nixpkgs that referenced this issue Apr 4, 2024
Bug upstream introduced in version 3.0.41 causes xonsh to randomly crash.
See xonsh/xonsh#5241
@gnat
Copy link

gnat commented Apr 22, 2024

Possible to just bundle the correct version of prompt_toolkit inside the AppImage? Or perhaps fix xonsh to work with the newer prompt_toolkit ?

This blows up for me with a simple echo $(ls) lol.

@anki-code
Copy link
Member

anki-code commented Apr 22, 2024

hi @gnat! Can you test and confirm this - #5241 (comment) ? Downgrading prompt toolkit solves the issue?

@gnat
Copy link

gnat commented Apr 22, 2024

#5241 (comment) is probably not a viable solution for the AppImage version of xonsh. See:

image

@anki-code
Copy link
Member

@gnat thanks for the test!

Your test is a bit not correct because you reinstall package without restart xonsh. And after restart I have no confidence that during the run xonsh will use the older version because newer one is packed to AppImage but you can try. Please read the section about running xonsh in the cheatsheet.

Meanwhile I created PR with pinning version for AppImage. See #5356.

@anki-code
Copy link
Member

@gnat I think you can unpack AppImage, reinstall ptk, and test:

cd /tmp
wget https://github.com/xonsh/xonsh/releases/download/0.15.1/xonsh-x86_64.AppImage
chmod +x xonsh-x86_64.AppImage
./xonsh-x86_64.AppImage --appimage-extract 
./xonsh-x86_64/bin/xonsh
xpip uninstall prompt-toolkit
xpip install prompt-toolkit==3.0.40
exit
./xonsh-x86_64/bin/xonsh

@gnat
Copy link

gnat commented Apr 23, 2024

@gforsyth possible to get a tag/release for a new appimage? Thank you!

@anki-code
Copy link
Member

@gnat new version have come, please test

@gnat
Copy link

gnat commented Apr 25, 2024

@anki-code No go, sadly, with 0.16 AppImage

image

@anki-code
Copy link
Member

anki-code commented Apr 25, 2024

@gnat please provide more details: xonfig output, operation system, any details you think helpful.

Workaround: ./xonsh --shell-type readline

@anki-code
Copy link
Member

anki-code commented Apr 25, 2024

@jnoortheen hey! I'm randomly getting the asyncio errors from ptk. Maybe it's related to ptk releases maybe to xonsh releases maybe to python 3.12 release. You had an afforts around using asyncio and I treat you as an expert in this. Could you please take a look into this? It's annoying issue. If you have not enough time could you please share your thoughts and maybe links to docs that I can read to have knowledge.

I've caught this now for my xonsh-installed env:

Exception ignored in: <function BaseEventLoop.__del__ at 0x106479ee0>
Traceback (most recent call last):
  File "/Users/pc/.local/xonsh-env/lib/python3.12/asyncio/base_events.py", line 726, in __del__
    self.close()
  File "/Users/pc/.local/xonsh-env/lib/python3.12/asyncio/unix_events.py", line 68, in close
    super().close()
  File "/Users/pc/.local/xonsh-env/lib/python3.12/asyncio/selector_events.py", line 104, in close
    self._close_self_pipe()
  File "/Users/pc/.local/xonsh-env/lib/python3.12/asyncio/selector_events.py", line 112, in _close_self_pipe
    self._ssock.close()
  File "/Users/pc/.local/xonsh-env/lib/python3.12/socket.py", line 504, in close
    self._real_close()
  File "/Users/pc/.local/xonsh-env/lib/python3.12/socket.py", line 498, in _real_close
    _ss.close(self)
OSError: [Errno 9] Bad file descriptor
echo 1 | asdTraceback (most recent call last):
  File "/Users/pc/.local/xonsh-env/lib/python3.12/site-packages/xonsh/main.py", line 470, in main
    sys.exit(main_xonsh(args))
             ^^^^^^^^^^^^^^^^
  File "/Users/pc/.local/xonsh-env/lib/python3.12/site-packages/xonsh/main.py", line 514, in main_xonsh
    shell.shell.cmdloop()
  File "/Users/pc/.local/xonsh-env/lib/python3.12/site-packages/xonsh/ptk_shell/shell.py", line 406, in cmdloop
    line = self.singleline(auto_suggest=auto_suggest)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pc/.local/xonsh-env/lib/python3.12/site-packages/xonsh/ptk_shell/shell.py", line 374, in singleline
    line = self.prompter.prompt(**prompt_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pc/.local/xonsh-env/lib/python3.12/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1024, in prompt
    return self.app.run(
           ^^^^^^^^^^^^^
  File "/Users/pc/.local/xonsh-env/lib/python3.12/site-packages/prompt_toolkit/application/application.py", line 973, in run
    return loop.run_until_complete(coro)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pc/.local/xonsh-env/lib/python3.12/asyncio/base_events.py", line 672, in run_until_complete
    self.run_forever()
  File "/Users/pc/.local/xonsh-env/lib/python3.12/asyncio/base_events.py", line 639, in run_forever
    self._run_once()
  File "/Users/pc/.local/xonsh-env/lib/python3.12/asyncio/base_events.py", line 1947, in _run_once
    event_list = self._selector.select(timeout)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pc/.local/xonsh-env/lib/python3.12/selectors.py", line 566, in select
    kev_list = self._selector.control(None, max_ev, timeout)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 9] Bad file descriptor
Xonsh encountered an issue during launch
Failback to /bin/zsh

UPD: I catched this exception again. I suppose that after another process was stopped.

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

No branches or pull requests

4 participants