Skip to content

Jobs - #14883

Merged
132ikl merged 71 commits into
nushell:mainfrom
cosineblast:jobs
Feb 25, 2025
Merged

Jobs#14883
132ikl merged 71 commits into
nushell:mainfrom
cosineblast:jobs

Conversation

@cosineblast

@cosineblast cosineblast commented Jan 21, 2025 •

Copy link
Copy Markdown
Contributor

Description

This is an attempt to improve the nushell situation with regard to issue #247.

This PR implements:

  • spawning jobs: job spawn { do_background_thing }
    Jobs will be implemented as threads and not forks, to maintain a consistent behavior between unix and windows.

  • listing running jobs: job list
    This should allow users to list what background tasks they currently have running.

  • killing jobs: job kill <id>

  • interupting nushell code in the job's background thread

  • interrupting the job's currently-running process, if any.

Things that should be taken into consideration for implementation:

  • (unix-only) Handling TSTP signals while executing code and turning the current program into a background job, and unfreezing them in foreground job unfreeze.

  • Ensuring processes spawned by background jobs get distinct process groups from the nushell shell itself

This PR originally aimed to implement some of the following, but it is probably ideal to be left for another PR (scope creep)

  • Disowning external process jobs (job dispatch)
  • Inter job communication (job send/recv)

Roadblocks encountered so far:

  • Nushell does some weird terminal sequence magics which make so that when a background process or thread prints something to stderr and the prompt is idle, the stderr output ends up showing up weirdly

@132ikl

132ikl commented Jan 21, 2025

Copy link
Copy Markdown
Member

looks interesting! by the way, if you haven't seen these PRs which also try to tackle background jobs, and might have some info on roadblocks you might run into. seems like the major blocker was the external printer in reedline.

#11696
nushell/reedline#737

@cosineblast

Copy link
Copy Markdown
Contributor Author

looks interesting! by the way, if you haven't seen these PRs which also try to tackle background jobs, and might have some info on roadblocks you might run into. seems like the major blocker was the external printer in reedline.

#11696 nushell/reedline#737

Yeah, the mentioned reedline output problems have already shown up. I'll have a look into it, thanks.

This implementation still does not kill the job's current process if it
is executing any, but already sends an interrupt through Signals which
allows the job to be interrupted if it is running nushell code.
We still need to implement unfreezing it after suspending the program,
and killing frozen jobs.

TSTP a unix-specific thing, that could possibly be *emulated* in windows
in the future, but for now, I think implementing this expected unix
shell behaviour is a good call.
This solves an issue that jobs were being silenced even when piped into
a Value, which is not desirable. This commit addresses and adds tests
for this behavior.
@cosineblast

cosineblast commented Feb 24, 2025 •

Copy link
Copy Markdown
Contributor Author

Oh well, it seems there was a mistake in my implementation for slient background jobs. The intended output would be something lke this:

job spawn { print "hi" }
# => 1
# => hi

Rationale: Even though background jobs are silenced on command output, an explicit print should still print things up.

job spawn { bash -c "echo 'hi'" }
# => 2
# No output from external

Rationale: background job terminal output is silenced

job spawn { print (^ls) }
# => 3
# => (output from ls)

Same rationale as case 1.

I pushed a fix and some tests to cover this behavior.

@IanManske

Copy link
Copy Markdown
Member

Sorry, missed your earlier changes, looks good on my end.

@132ikl

132ikl commented Feb 25, 2025

Copy link
Copy Markdown
Member

looks like we're good to go! thank you again so much for working on this, we're all eager to give it a shot 😄

@132ikl
132ikl merged commit 9bb7f0c into nushell:main Feb 25, 2025
@github-actions github-actions Bot added this to the v0.103.0 milestone Feb 25, 2025
@fdncred

fdncred commented Feb 25, 2025

Copy link
Copy Markdown
Contributor

@cosineblast Congrats on landing a PR that we've wanted for over 5 years now. Way to go!

First question, is this a bug or intended behavior.

On Windows, if I do job spawn { notepad } it lauches notepad, yay! Then if I do job list nothing shows up, even though notepad is still running. I assumed I'd get an id listing that I could try job kill on.

@cosineblast

Copy link
Copy Markdown
Contributor Author

Well, that's mostly intentional. Some GUI programs like firefox on linux and notepad on windows do not block the terminal (by spawning the actual graphical application in another child process). In that case, the job will terminate since the new process just terminated

@WindSoilder

Copy link
Copy Markdown
Contributor

@cosineblast Thank you for implementing such amazing feature!

@cosineblast

Copy link
Copy Markdown
Contributor Author

You're welcome, I'm very glad to help. I plan on improving this feature in the future (e.g adding messaging, improving tests robustness, etc), but for the time being I think this is enough

@sholderbach

Copy link
Copy Markdown
Member

Thanks for all the work!

I saw a test failure in commands::job::killing_job_kills_pids on a different branch, that may be flaky

https://github.com/nushell/nushell/actions/runs/13544229685/job/37851947329

@NotTheDr01ds

Copy link
Copy Markdown
Contributor

I saw a test failure

I've seen the same intermittent test failure locally as well as in one other test. In both cases, it seemed to be a timing issue, and locally modifying the sleep duration fixed it. I'll keep an eye out for it happening again.

@cosineblast

Copy link
Copy Markdown
Contributor Author

I'll experiment on improving them with some better inter-job communication mechanisms soon, thanks for pointing those issues out

@nyabinary

Copy link
Copy Markdown

Are hotkeys/shortcuts going to be impl as well in the future?

@sholderbach sholderbach added the A:job-control The system to manage background jobs and concurrent tasks label Mar 21, 2025
@sholderbach

Copy link
Copy Markdown
Member

Are hotkeys/shortcuts going to be impl as well in the future?

Do you have particular shortcuts in mind? Feel free to open an enhancement issue, we can put it under the job-control label

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

Labels

A:job-control The system to manage background jobs and concurrent tasks notes:mention Noted in "Hall of Fame" section

Projects

None yet

Development

Successfully merging this pull request may close these issues.