Repository: xonsh/xonsh

Issue:
backgrounding certain processes broken
Similar to #2071 when I try and background some commands e.g. sleep, gedit -s I do not get the shell prompt back.  vim and top work as expected.
```
~  sleep 30
^Z
fg
```
where ~ is the shell prompt

[pip_freeze_user.txt](https://github.com/xonsh/xonsh/files/905207/pip_freeze_user.txt)

## For community
⬇️  **Please click the 👍 reaction instead of leaving a `+1` or 👍  comment**



Discussion:
It's kind of a flaw thing in current xonsh, which I think needs to improve. When running programs, xonsh by default will use thread to run it. While signals like Ctrl-Z can only handled by the main thread, IIRC.

vim and top works here is because they are in a special list marked as "unthreadble". So the solution to this issue is adding "sleep" to [this list](https://github.com/xonsh/xonsh/blob/a891b21ab3d640b9af698a6aac4ff61bdbdf7540/xonsh/commands_cache.py#L375) too.
What's the advantage of using a thread, speed?  Perhaps it should default to forking and whitelist threadable?
@stan3 -- short answer: we use threads because we have to given what we want xonsh to do

long answer: https://github.com/xonsh/xonsh/pull/1758#issuecomment-251032267

The whitelisting thing will hopefully be obviated by @astronouth7303's xonsh/slug project
I find when I push Ctrl+Z to background a process, it hangs the process and xonsh. The only way to restore order is to open a new terminal, and then `kill -9` the process (which I actually wanted to keep). Seen identical results on OSX and Debian Stretch. Is there any way of disabling Ctrl+Z until this can be fixed?
same here. Ctrl-Z doesn't work with 0.59. Have to kill the process. 
Still happening for me with `cat` in `0.6.0`.
I'm hitting this as well.
Hmm interesting. I am also seeing this.
Is there any news on this? I am revisiting xonsh now, looking at its state to decide whether I should use it regularly. 

In version 0.7.9 xonsh doesn't seem to handle ctrl-z (suspending) followed by bg the same way bash does:

emacs
ctrl-z  (suspends emacs, OK - shows ^Z^Z for some reason)
bg (doesn't detach emacs, rather seems to bring it to the foreground - like fg)
 
Also, ctrl-z after cat > xxx doesn't seem to work at all (and after ctrl-z, ctrl-c doesn't work and one has to kill xonsh externally)


I don't think a lot of progress has been made on this
Job control is central to command shell functionality. Is there a fundamental implementation reason that blocks 100% bash compatible job handling in xonsh?
@mburo -- see @scopatz comments on the subprocess refactor here: https://github.com/xonsh/xonsh/pull/1758#issuecomment-251032267
If you try to ctrl-Z a process and it hangs, you don't actually have to `kill -9` it. The process is in the "stopped" state. You can continue it with `kill -SIGCONT`. There's still no way to send the process to the background though.

Resolving patch:
diff --git a/xonsh/jobs.py b/xonsh/jobs.py
--- a/xonsh/jobs.py
+++ b/xonsh/jobs.py
@@ -383,7 +383,7 @@ def jobs(args, stdin=None, stdout=sys.stdout, stderr=None):
     return None, None
 
 
-def resume_job(args, wording):
+def resume_job(args, wording: tp.Literal["fg", "bg"]):
     """
     used by fg and bg to resume a job either in the foreground or in the background.
     """
@@ -419,7 +419,9 @@ def resume_job(args, wording):
     if XSH.env.get("XONSH_INTERACTIVE"):
         print_one_job(tid)
     pipeline = job["pipeline"]
-    pipeline.resume(job)
+    pipeline.resume(
+        job, tee_output=(wording == "fg")
+    )  # do not tee output for background jobs
 
 
 @unthreadable


Test patch: