Repository: xonsh/xonsh

Issue:
PTK OSError Bad file descriptor
When I run a command like `cat foo.json | py -m json.tool`, xonsh got crash with following errors after the command pipe finish (with output correct). I'm not sure it's a ptk issue or xonsh one. Either way I think we should add some `try-catch` for not crashing the shell at least and for stability.

```
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/xonsh/ptk/shortcuts.py", line 108, in prompt
    document = cli.run(reset_current_buffer=False)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/prompt_toolkit/interface.py", line 394, in run
    self.eventloop.run(self.input, self.create_eventloop_callbacks())
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/prompt_toolkit/eventloop/posix.py", line 93, in run
    self.add_reader(self._schedule_pipe[0], None)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/prompt_toolkit/eventloop/posix.py", line 280, in add_reader
    self.selector.register(fd)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/prompt_toolkit/eventloop/select.py", line 75, in register
    sel.register(fd)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/prompt_toolkit/eventloop/select.py", line 130, in register
    self._sel.register(fd, selectors.EVENT_READ, None)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/selectors.py", line 536, in register
    self._kqueue.control([kev], 0, 0)
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/bin/xonsh", line 3, in <module>
    main()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/xonsh/main.py", line 276, in main
    shell.shell.cmdloop()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/xonsh/ptk/shell.py", line 132, in cmdloop
    line = self.singleline(auto_suggest=auto_suggest)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/xonsh/ptk/shell.py", line 101, in singleline
    line = self.prompter.prompt(**prompt_args)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/xonsh/ptk/shortcuts.py", line 113, in prompt
    eventloop.close()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/prompt_toolkit/eventloop/posix.py", line 270, in close
    os.close(schedule_pipe[0])
OSError: [Errno 9] Bad file descriptor
```


Discussion:
Hi @mitnk - thanks for reporting! I cannot reproduce this on Linux. But I agree that we should do something to fix it.  This does seem to be a PTK issue, for what it is worth. Would you mind reporting it there?

Resolving patch:
diff --git a/xonsh/ptk/shortcuts.py b/xonsh/ptk/shortcuts.py
--- a/xonsh/ptk/shortcuts.py
+++ b/xonsh/ptk/shortcuts.py
@@ -9,6 +9,7 @@
                                       create_eventloop, create_asyncio_eventloop, create_output)
 
 from xonsh.platform import ptk_version_info
+import xonsh.tools as xt
 
 
 class Prompter(object):
@@ -106,9 +107,13 @@ def prompt_coro():
             try:
                 with patch_context:
                     document = cli.run(reset_current_buffer=False)
-
                     if document:
                         return document.text
+            except Exception:
+                xt.print_exception()
+                # return something to prevent xonsh crash when any
+                # exceptions raise
+                return ''
             finally:
                 eventloop.close()
 


Test patch: