Code Monkey home page Code Monkey logo

processx's Introduction

processx

Execute and Control System Processes

lifecycle R-CMD-check CRAN RStudio mirror downloads Codecov test coverage

Tools to run system processes in the background, read their standard output and error and kill them.

processx can poll the standard output and error of a single process, or multiple processes, using the operating system’s polling and waiting facilities, with a timeout.


Features

  • Start system processes in the background and find their process id.
  • Read the standard output and error, using non-blocking connections
  • Poll the standard output and error connections of a single process or multiple processes.
  • Write to the standard input of background processes.
  • Check if a background process is running.
  • Wait on a background process, or multiple processes, with a timeout.
  • Get the exit status of a background process, if it has already finished.
  • Kill background processes.
  • Kill background process, when its associated object is garbage collected.
  • Kill background processes and all their child processes.
  • Works on Linux, macOS and Windows.
  • Lightweight, it only depends on the also lightweight R6 and ps packages.

Installation

Install the stable version from CRAN:

install.packages("processx")

If you need the development version, install it from GitHub:

pak::pak("r-lib/processx")

Usage

library(processx)

Note: the following external commands are usually present in macOS and Linux systems, but not necessarily on Windows. We will also use the px command line tool (px.exe on Windows), that is a very simple program that can produce output to stdout and stderr, with the specified timings.

px <- paste0(
  system.file(package = "processx", "bin", "px"),
  system.file(package = "processx", "bin", .Platform$r_arch, "px.exe")
)
px
#> [1] "/Users/gaborcsardi/Library/R/arm64/4.2/library/processx/bin/px"

Running an external process

The run() function runs an external command. It requires a single command, and a character vector of arguments. You don’t need to quote the command or the arguments, as they are passed directly to the operating system, without an intermediate shell.

run("echo", "Hello R!")
#> $status
#> [1] 0
#> 
#> $stdout
#> [1] "Hello R!\n"
#> 
#> $stderr
#> [1] ""
#> 
#> $timeout
#> [1] FALSE

Short summary of the px binary we are using extensively below:

result <- run(px, "--help", echo = TRUE)
#> Usage: px [command arg] [command arg] ...
#> 
#> Commands:
#>   sleep  <seconds>           -- sleep for a number os seconds
#>   out    <string>            -- print string to stdout
#>   err    <string>            -- print string to stderr
#>   outln  <string>            -- print string to stdout, add newline
#>   errln  <string>            -- print string to stderr, add newline
#>   errflush                   -- flush stderr stream
#>   cat    <filename>          -- print file to stdout
#>   return <exitcode>          -- return with exitcode
#>   writefile <path> <string>  -- write to file
#>   write <fd> <string>        -- write to file descriptor
#>   echo <fd1> <fd2> <nbytes>  -- echo from fd to another fd
#>   getenv <var>               -- environment variable to stdout

Note: From version 3.0.1, processx does not let you specify a full shell command line, as this involves starting a grandchild process from the child process, and it is difficult to clean up the grandchild process when the child process is killed. The user can still start a shell (sh or cmd.exe) directly of course, and then proper cleanup is the user’s responsibility.

Errors

By default run() throws an error if the process exits with a non-zero status code. To avoid this, specify error_on_status = FALSE:

run(px, c("out", "oh no!", "return", "2"), error_on_status = FALSE)
#> $status
#> [1] 2
#> 
#> $stdout
#> [1] "oh no!"
#> 
#> $stderr
#> [1] ""
#> 
#> $timeout
#> [1] FALSE

Showing output

To show the output of the process on the screen, use the echo argument. Note that the order of stdout and stderr lines may be incorrect, because they are coming from two different connections.

result <- run(px,
  c("outln", "out", "errln", "err", "outln", "out again"),
  echo = TRUE)
#> out
#> out again
#> err

If you have a terminal that support ANSI colors, then the standard error output is shown in red.

The standard output and error are still included in the result of the run() call:

result
#> $status
#> [1] 0
#> 
#> $stdout
#> [1] "out\nout again\n"
#> 
#> $stderr
#> [1] "err\n"
#> 
#> $timeout
#> [1] FALSE

Note that run() is different from system(), and it always shows the output of the process on R’s proper standard output, instead of writing to the terminal directly. This means for example that you can capture the output with capture.output() or use sink(), etc.:

out1 <- capture.output(r1 <- system("ls"))
out2 <- capture.output(r2 <- run("ls", echo = TRUE))
out1
#> character(0)
out2
#>  [1] "CODE_OF_CONDUCT.md" "DESCRIPTION"        "LICENSE"           
#>  [4] "LICENSE.md"         "Makefile"           "NAMESPACE"         
#>  [7] "NEWS.md"            "R"                  "README.Rmd"        
#> [10] "README.md"          "_pkgdown.yml"       "codecov.yml"       
#> [13] "inst"               "man"                "processx.Rproj"    
#> [16] "src"                "tests"

Spinner

The spinner option of run() puts a calming spinner to the terminal while the background program is running. The spinner is always shown in the first character of the last line, so you can make it work nicely with the regular output of the background process if you like. E.g. try this in your R terminal:

result <- run(px,
  c("out", "  foo",
    "sleep", "1",
    "out", "\r  bar",
    "sleep", "1",
    "out", "\rX foobar\n"),
  echo = TRUE, spinner = TRUE)

Callbacks for I/O

run() can call an R function for each line of the standard output or error of the process, just supply the stdout_line_callback or the stderr_line_callback arguments. The callback functions take two arguments, the first one is a character scalar, the output line. The second one is the process object that represents the background process. (See more below about process objects.) You can manipulate this object in the callback, if you want. For example you can kill it in response to an error or some text on the standard output:

cb <- function(line, proc) {
  cat("Got:", line, "\n")
  if (line == "done") proc$kill()
}
result <- run(px,
  c("outln", "this", "outln", "that", "outln", "done",
    "outln", "still here", "sleep", "10", "outln", "dead by now"), 
  stdout_line_callback = cb,
  error_on_status = FALSE,
)
#> Got: this 
#> Got: that 
#> Got: done 
#> Got: still here
result
#> $status
#> [1] -9
#> 
#> $stdout
#> [1] "this\nthat\ndone\nstill here\n"
#> 
#> $stderr
#> [1] ""
#> 
#> $timeout
#> [1] FALSE

Keep in mind, that while the R callback is running, the background process is not stopped, it is also running. In the previous example, whether still here is printed or not depends on the scheduling of the R process and the background process by the OS. Typically, it is printed, because the R callback takes a while to run.

In addition to the line-oriented callbacks, the stdout_callback and stderr_callback arguments can specify callback functions that are called with output chunks instead of single lines. A chunk may contain multiple lines (separated by \n or \r\n), or even incomplete lines.

Managing external processes

If you need better control over possibly multiple background processes, then you can use the R6 process class directly.

Starting processes

To start a new background process, create a new instance of the process class.

p <- process$new("sleep", "20")

Killing a process

A process can be killed via the kill() method.

p$is_alive()
#> [1] TRUE
p$kill()
#> [1] TRUE
p$is_alive()
#> [1] FALSE

Note that processes are finalized (and killed) automatically if the corresponding process object goes out of scope, as soon as the object is garbage collected by R:

p <- process$new("sleep", "20")
rm(p)
invisible(gc())

Here, the direct call to the garbage collector kills the sleep process as well. See the cleanup option if you want to avoid this behavior.

Standard output and error

By default the standard output and error of the processes are ignored. You can set the stdout and stderr constructor arguments to a file name, and then they are redirected there, or to "|", and then processx creates connections to them. (Note that starting from processx 3.0.0 these connections are not regular R connections, because the public R connection API was retroactively removed from R.)

The read_output_lines() and read_error_lines() methods can be used to read complete lines from the standard output or error connections. They work similarly to the readLines() base R function.

Note, that the connections have a buffer, which can fill up, if R does not read out the output, and then the process will stop, until R reads the connection and the buffer is freed.

Always make sure that you read out the standard output and/or error of the pipes, otherwise the background process will stop running!

If you don’t need the standard output or error any more, you can also close it, like this:

close(p$get_output_connection())
close(p$get_error_connection())

Note that the connections used for reading the output and error streams are non-blocking, so the read functions will return immediately, even if there is no text to read from them. If you want to make sure that there is data available to read, you need to poll, see below.

p <- process$new(px,
  c("sleep", "1", "outln", "foo", "errln", "bar", "outln", "foobar"),
  stdout = "|", stderr = "|")
p$read_output_lines()
#> character(0)
p$read_error_lines()
#> character(0)

End of output

The standard R way to query the end of the stream for a non-blocking connection, is to use the isIncomplete() function. After a read attempt, this function returns FALSE if the connection has surely no more data. (If the read attempt returns no data, but isIncomplete() returns TRUE, then the connection might deliver more data in the future.

The is_incomplete_output() and is_incomplete_error() functions work similarly for process objects.

Polling the standard output and error

The poll_io() method waits for data on the standard output and/or error of a process. It will return if any of the following events happen:

  • data is available on the standard output of the process (assuming there is a connection to the standard output).
  • data is available on the standard error of the process (assuming the is a connection to the standard error).
  • The process has finished and the standard output and/or error connections were closed on the other end.
  • The specified timeout period expired.

For example the following code waits about a second for output.

p <- process$new(px, c("sleep", "1", "outln", "kuku"), stdout = "|")

## No output yet
p$read_output_lines()
#> character(0)
## Wait at most 5 sec
p$poll_io(5000)
#>   output    error  process 
#>  "ready" "nopipe" "nopipe"
## There is output now
p$read_output_lines()
#> [1] "kuku"

Polling multiple processes

If you need to manage multiple background processes, and need to wait for output from all of them, processx defines a poll() function that does just that. It is similar to the poll_io() method, but it takes multiple process objects, and returns as soon as one of them have data on standard output or error, or a timeout expires. Here is an example:

p1 <- process$new(px, c("sleep", "1", "outln", "output"), stdout = "|")
p2 <- process$new(px, c("sleep", "2", "errln", "error"), stderr = "|")

## After 100ms no output yet
poll(list(p1 = p1, p2 = p2), 100)
#> $p1
#>    output     error   process 
#> "timeout"  "nopipe"  "nopipe" 
#> 
#> $p2
#>    output     error   process 
#>  "nopipe" "timeout"  "nopipe"
## But now we surely have something
poll(list(p1 = p1, p2 = p2), 1000)
#> $p1
#>   output    error  process 
#>  "ready" "nopipe" "nopipe" 
#> 
#> $p2
#>   output    error  process 
#> "nopipe" "silent" "nopipe"
p1$read_output_lines()
#> [1] "output"
## Done with p1
close(p1$get_output_connection())
#> NULL
## The second process should have data on stderr soonish
poll(list(p1 = p1, p2 = p2), 5000)
#> $p1
#>   output    error  process 
#> "closed" "nopipe" "nopipe" 
#> 
#> $p2
#>   output    error  process 
#> "nopipe"  "ready" "nopipe"
p2$read_error_lines()
#> [1] "error"

Waiting on a process

As seen before, is_alive() checks if a process is running. The wait() method can be used to wait until it has finished (or a specified timeout expires).. E.g. in the following code wait() needs to wait about 2 seconds for the sleep px command to finish.

p <- process$new(px, c("sleep", "2"))
p$is_alive()
#> [1] TRUE
Sys.time()
#> [1] "2022-06-10 13:57:49 CEST"
p$wait()
Sys.time()
#> [1] "2022-06-10 13:57:51 CEST"

It is safe to call wait() multiple times:

p$wait() # already finished!

Exit statuses

After a process has finished, its exit status can be queried via the get_exit_status() method. If the process is still running, then this method returns NULL.

p <- process$new(px, c("sleep", "2"))
p$get_exit_status()
#> NULL
p$wait()
p$get_exit_status()
#> [1] 0

Mixing processx and the parallel base R package

In general, mixing processx (via callr or not) and parallel works fine. If you use parallel’s ‘fork’ clusters, e.g. via parallel::mcparallel(), then you might see two issues. One is that processx will not be able to determine the exit status of some processx processes. This is because the status is read out by parallel, and processx will set it to NA. The other one is that parallel might complain that it could not clean up some subprocesses. This is not an error, and it is harmless, but it does hold up R for about 10 seconds, before parallel gives up. To work around this, you can set the PROCESSX_NOTIFY_OLD_SIGCHLD environment variable to a non-empty value, before you load processx. This behavior might be the default in the future.

Errors

Errors are typically signalled via non-zero exits statuses. The processx constructor fails if the external program cannot be started, but it does not deal with errors that happen after the program has successfully started running.

p <- process$new("nonexistant-command-for-sure")
#> Error in c("process_initialize(self, private, command, args, stdin, stdout, ", : ! Native call to `processx_exec` failed
#> Caused by error in `chain_call(c_processx_exec, command, c(command, args), pty, pty_options, …` at initialize.R:138:3:
#> ! cannot start processx process 'nonexistant-command-for-sure' (system error 2, No such file or directory) @unix/processx.c:613 (processx_exec)
p2 <- process$new(px, c("sleep", "1", "command-does-not-exist"))
p2$wait()
p2$get_exit_status()
#> [1] 5

Related tools

  • The ps package can query, list, manipulate all system processes (not just subprocesses), and processx uses it internally for some of its functionality. You can also convert a processx::process object to a ps::ps_handle with the as_ps_handle() method.

  • The callr package uses processx to start another R process, and run R code in it, in the foreground or background.

Code of Conduct

Please note that the processx project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

License

MIT © Ascent Digital Services, RStudio, Gábor Csárdi

processx's People

Contributors

davechilders avatar dominik-handler avatar gaborcsardi avatar hadley avatar hugomflavio avatar infotroph avatar ironistm avatar jdblischak avatar jeroen avatar kendonb avatar kevinushey avatar lionel- avatar maelle avatar rschuchmann avatar salim-b avatar smu-ggl avatar wch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

processx's Issues

Clean up the whole process subtree

This is especially important for commandline invocations, where there is an intermediate shell. Currently we only clean up this shell.

On windows we can use job objects, and on Linux, maybe we can use process groups.

Trouble compiling under Window with RTools

Any help with compilation trouble on Windows? I just installed the latest RBuildTools v3.3 and then attempted to install processx as part of the devtools dependencies.

Thanks in advance.

> devtools::install_github("r-pkgs/processx")
Downloading GitHub repo r-pkgs/processx@master
from URL https://api.github.com/repos/r-pkgs/processx/zipball/master
Installing processx
"C:/PROGRA~1/R/R-32~1.2/bin/x64/R" --no-site-file --no-environ --no-save --no-restore CMD INSTALL  \
  "C:/Users/dkulp/AppData/Local/Temp/RtmpQBgJgU/devtools3b146a6928/r-pkgs-processx-ecee9d4"  \
  --library="C:/Users/dkulp/Documents/R/win-library/3.2" --install-tests 

* installing *source* package 'processx' ...
** libs
gcc -m64 -I"C:/PROGRA~1/R/R-32~1.2/include" -DNDEBUG     -I"d:/RCompile/r-compiling/local/local320/include"     -O2 -Wall  -std=gnu99 -mtune=core2 -c init.c -o init.o
gcc -m64 -I"C:/PROGRA~1/R/R-32~1.2/include" -DNDEBUG     -I"d:/RCompile/r-compiling/local/local320/include"     -O2 -Wall  -std=gnu99 -mtune=core2 -c unix.c -o unix.o
gcc -m64 -I"C:/PROGRA~1/R/R-32~1.2/include" -DNDEBUG     -I"d:/RCompile/r-compiling/local/local320/include"     -O2 -Wall  -std=gnu99 -mtune=core2 -c unix_poll.c -o unix_poll.o
gcc -m64 -I"C:/PROGRA~1/R/R-32~1.2/include" -DNDEBUG     -I"d:/RCompile/r-compiling/local/local320/include"     -O2 -Wall  -std=gnu99 -mtune=core2 -c utils.c -o utils.o
gcc -m64 -I"C:/PROGRA~1/R/R-32~1.2/include" -DNDEBUG     -I"d:/RCompile/r-compiling/local/local320/include"     -O2 -Wall  -std=gnu99 -mtune=core2 -c windows-stdio.c -o windows-stdio.o
windows-stdio.c:8:3: error: unknown type name '_In_'
windows-stdio.c:9:3: error: unknown type name '_In_opt_'
windows-stdio.c: In function 'processx__con_destroy':
windows-stdio.c:160:7: warning: implicit declaration of function 'CancelIoEx' [-Wimplicit-function-declaration]
make: *** [windows-stdio.o] Error 1
Warning: running command 'make -f "C:/PROGRA~1/R/R-32~1.2/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-32~1.2/share/make/winshlib.mk" SHLIB="processx.dll" WIN=64 TCLBIN=64 OBJECTS="init.o unix.o unix_poll.o utils.o windows-stdio.o windows.o windows_poll.o"' had status 2
ERROR: compilation failed for package 'processx'

Allow termination from RStudio

Is there any way we could make this killable from RStudio?

processx::run("sleep", "20")

I don't know whether this a something processx can do (by listening for interrupts and propagating?) or whether it's something that RStudio needs to do, or both.

cc @wch @kevinushey

Issues restarting rstudio

If I have a processx process running initiated from RStudio and RStudio is restarted there are issues killing processes/child processes. For example in RStudio:

library(webdriver)
phantom <- run_phantomjs()

Now if we select session -> Restart R

On windows RStudio client we experience a crash. On RStudio server running on Linux a complete freeze is experienced.

Win 10, R (2016-09-23 r71344) , RStudio 0.99.903
Ubuntu 16.04, R (3.3.1), rstudio-server 0.99.902

Set directory for process

Right now, if you want to start a process in a directory other than R's current dir, you need to call setwd() before starting the process, and then call it again afterward. And for extra safety, you would want to call setwd() in an on.exit, in case there's an error.

  old_dir <- setwd(new_dir)
  # In case we have an error in process$new(), use on.exit
  on.exit(setwd(old_dir))
  p <- process$new("ls", stdout = "|")$wait()
  setwd(old_dir)

It would be nice if there were an option to process$new that let you set the working directory.

Quoted command on Windows fails to start process

On Windows, if there are quotes around the command (when using commandline), then it fails to start the process correctly. Or at least the process exits with an error. For example:

library(processx)
p <- process$new(commandline = "echo abc")
p$get_exit_status() # 0

p <- process$new(commandline = '"echo" abc')
Sys.sleep(0.25)
p$get_exit_status() # 1

# Works fine with system()
system('"echo" abc') # abc

Send interrupt (hard on windows)

In addition to $kill() it would be nice to have $interrupt().

On unix/macos this can be implemented as

process_interrupt <- function(self, private) {
    "!DEBUG process_interrupt '`private$get_short_name()`', pid `private$pid`"
    if (!is.null(private$pid)) {
        pids <- c(private$pid, get_pid_tree(private$pid))
        pskill(pids, SIGINT)
    }
    invisible(self)
}

but on windows I think that this will kill the process

     Only ‘SIGINT’ and ‘SIGKILL’ will be defined on Windows, and
     ‘pskill’ will always use the Windows system call
     ‘TerminateProcess’.

(from ?pskill). Googleing around suggests that this is hard, if not impossible, to do on windows 😢

Wrong pid on Linux

and this has a lot of bad consequences:

  • wrong process killed on kil()
  • is_alive() is wrong
  • zombie and orphaned processes

We need to search the children of the R process only.

Also, we need to clean up the children recursively.

processx__child_init should close file descriptors

I believe that between the fork() and execvp(), file descriptors need to be closed. Otherwise the child process inherits them and this can cause problems.

For example (running R from the terminal):

library(processx)
x <- file('foo', 'w')
p <- process$new('sleep', '1000', cleanup=F, commandline=NULL)

Then, in another terminal:

$ lsof -c R -c sleep
...
R       51621 winston    0u      CHR               16,2 0t2262526     639 /dev/ttys002
R       51621 winston    1u      CHR               16,2 0t2262526     639 /dev/ttys002
R       51621 winston    2u      CHR               16,2 0t2262526     639 /dev/ttys002
R       51621 winston    3w      REG                1,3         0 8455255 /Users/winston/processx/foo
...
sleep   51625 winston    0r      CHR                3,2       0t0     302 /dev/null
sleep   51625 winston    1u      REG                1,3         0 8455758 /private/var/folders/vd/0_g4hj6d7kq_fw5gd_r0ml5w0000gn/T/RtmpnuvniE/filec9a546d26018
sleep   51625 winston    2u      REG                1,3         0 8455759 /private/var/folders/vd/0_g4hj6d7kq_fw5gd_r0ml5w0000gn/T/RtmpnuvniE/filec9a51da2ebfb
sleep   51625 winston    3w      REG                1,3         0 8455255 /Users/winston/processx/foo
sleep   51625 winston    6r      CHR                3,2       0t0     302 /dev/null
sleep   51625 winston    7u      REG                1,3         0 8455758 /private/var/folders/vd/0_g4hj6d7kq_fw5gd_r0ml5w0000gn/T/RtmpnuvniE/filec9a546d26018
sleep   51625 winston    8u      REG                1,3         0 8455759 /private/var/folders/vd/0_g4hj6d7kq_fw5gd_r0ml5w0000gn/T/RtmpnuvniE/filec9a51da2ebfb

Notice that file descriptor for /Users/winston/processx/foo is still held by the sleep process.

(@gaborcsardi: also, are fd's 6, 7, and 8 supposed to be duplicates of 0, 1, and 2?)

I believe that this causes RStudio on Mac to hang when you restart the R session, because the new R process is trying to use some resources held by the old sleep process. When you run the same code in RStudio, then run lsof, it shows this:

$ lsof -c rsession -c sleep
...
sleep    51721 winston    0r     CHR                3,2       0t0     302 /dev/null
sleep    51721 winston    1u     REG                1,3         0 8456491 /private/var/folders/vd/0_g4hj6d7kq_fw5gd_r0ml5w0000gn/T/RtmpsHcJbQ/filec9e52c5d1199
sleep    51721 winston    2u     REG                1,3         0 8456492 /private/var/folders/vd/0_g4hj6d7kq_fw5gd_r0ml5w0000gn/T/RtmpsHcJbQ/filec9e5122e6a90
sleep    51721 winston    6u    IPv4 0xaf88f42998bf875f       0t0     TCP localhost:17718 (LISTEN)
sleep    51721 winston    7w     REG                1,3         0 8456490 /Users/winston/processx/foo
sleep    51721 winston    8u     REG                1,3         0 7778604 /Users/winston/processx/.Rproj.user/C7FDA9A9/sources/s-38D90037/lock_file
sleep    51721 winston    9     PIPE 0xaf88f429987d9717     16384         ->0xaf88f429987d7cd7
sleep    51721 winston   10r     CHR                3,2       0t0     302 /dev/null
sleep    51721 winston   13     PIPE 0xaf88f429987d7497     16384         ->0xaf88f429987d9ad7
sleep    51721 winston   16u     REG                1,3         0 8456491 /private/var/folders/vd/0_g4hj6d7kq_fw5gd_r0ml5w0000gn/T/RtmpsHcJbQ/filec9e52c5d1199
sleep    51721 winston   17     PIPE 0xaf88f429987d9ad7     16384         ->0xaf88f429987d7497
sleep    51721 winston   18     PIPE 0xaf88f429987d9c57     16384         ->0xaf88f429987d82d7
sleep    51721 winston   19     PIPE 0xaf88f429987d82d7     16384         ->0xaf88f429987d9c57
sleep    51721 winston   20u    IPv4 0xaf88f42998d12e67       0t0     TCP localhost:15736 (LISTEN)
sleep    51721 winston   21u     REG                1,3         0 8456492 /private/var/folders/vd/0_g4hj6d7kq_fw5gd_r0ml5w0000gn/T/RtmpsHcJbQ/filec9e5122e6a90
sleep    51721 winston   22     PIPE 0xaf88f429987d9597     65536         ->0xaf88f429987d7d97
sleep    51721 winston   24     PIPE 0xaf88f429987d9b97     16384         ->0xaf88f429987d8f97
...

R's system() and system2() functions seem to do the same thing, unfortunately.

cc @jcheng5, @kevinushey, @jmcphers

Signal startup errors

This is not easy, because pipe() returns cleanly. Then we can check if the process is already running, but we cannot be sure that the shell had a chance to start it already. So it might not be running yet.

We need a better startup procedure for this, unfortunately.

supervisor_kill can leave behind zombies

Running supervisor_kill() can leave behind zombie processes, on both Mac and Linux:

library(processx)
process$new('sleep', args='1000', commandline=NULL, cleanup=F, supervise=T)
process$new('sleep', args='1000', commandline=NULL, cleanup=F, supervise=T)
process$new('sleep', args='1000', commandline=NULL, cleanup=F, supervise=T)
process$new('sleep', args='1000', commandline=NULL, cleanup=F, supervise=T)
process$new('sleep', args='1000', commandline=NULL, cleanup=F, supervise=T)
processx:::supervisor_kill()

Running ps after the R code above:

$ ps aux | grep sleep
winston          60850   0.0  0.0        0      0   ??  Z     4:33PM   0:00.00 (sleep)
winston          60848   0.0  0.0        0      0   ??  Z     4:33PM   0:00.00 (sleep)
winston          60862   0.0  0.0  2432804   2000 s004  S+    4:34PM   0:00.00 grep sleep

supervisor_kill() usually runs only when R exits, and so it won't leave behind zombie processes, but even so, this should not happen.

When supervisor_kill() is called, this code that sends SIGTERMs is run: https://github.com/r-pkgs/processx/blob/a60d83a/src/supervisor/supervisor.c#L144-L153

Is it possible that this is happening because the SIGCHLD events are coming very close together and the SIGCHLD handler can't handle them that quickly?

cleanup no longer survives cleanup

I am trying to use processx to launch a background process that I can save the pid for. On master I can specify cleanup = FALSE and the processx generated process survives the death of the parent process. With the current code on c that does not happen (as of ef93c72)

px <- processx::process$new("sleep", "100000", cleanup = FALSE)
px$is_alive()
(pid <- px$get_pid())
# process alive - the R docs are wrong here - FALSE means alive :(
(tools::pskill(pid, 0))
rm(px)
gc()
# process has died
(tools::pskill(pid, 0))

Supply standard input

The best would be if we could write to it from R, but it is unlikely that I can implement that on windows.

Error from process$new

Getting an error when I try to run the process$new example:

process$new("sleep", "20")

Error in wait_for_file(pidfile3) :
File was not created in 10 secs:

Getting the same error on two different windows machines, haven't tested on other OS

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.