actions / toolkit

The GitHub ToolKit for developing GitHub Actions.
https://github.com/features/actions
MIT License
5.01k stars 1.44k forks source link

exec - Support for detachable processes #715

Open m4c0 opened 3 years ago

m4c0 commented 3 years ago

Describe the enhancement Add support to exec background processes that can be detached from the NodeJS process, effectively allowing them to outlive the action step.

Code Snippet This is the expected usage in JS:

const { exec } = require('@actions/exec');
exec(process, args, {
  detached: true,
});

Additional information I'm doing some experiments with a custom GitHub Action to run unit tests in an Android emulator. I want to do some setup, start a process in the background and run action steps that requires the background process. This concept works if I do it as a series of run in a workflow (as simple as run: command &).

When I tried to reproduce it with a custom action, I noticed the spawned process never detaches from the parent and the whole action freezes. I tried to do a nohup process & on Ubuntu and the result was the same.

After some trial-and-error, I was able to properly detach my process using this snippet in my node_modules copy of _getSpawnOptions in toolrunner.js:

result.detached = options.detached;
if (options.detached) {
  result.stdio = 'ignore';
}

Ignoring stdio is mandatory, otherwise the pipes will also wait on the process. The whole thing is also doable if I replace exec with child_process, but I think exec users may benefit from the detach. I only need it for Ubuntu but NodeJS claims it works in Windows as well.

I can submit a PR if this approach is approved. I want to refactor out the getSpawnOptions method to add that option, as well as unit test it - I think that approach allows exec to expose more child_process.spawn if needed, without requiring process-spawning tests.

vandot commented 2 years ago

Any updates on this?

orizerah commented 1 year ago

Any updates on this?