Simple Shiny app with invalidateLater() incorrectly drops persistent background processes

154 views Asked by At

I am trying to write a minimal Shiny app that maintains a persistent external background process. For reasons specific to the full-sized use case, I am tracking the PID in a text file instead of just using the processx handle. When I start the app, it looks like this:

enter image description here

When I press the "start" button, the app creates a background process and records the PID in a text file. A reactive context with invalidateLater() repeatedly reads the text file and shows the PID and status.

enter image description here

The process is supposed to run until I click "stop". But less than a second after initialization, the process quits on its own.

enter image description here

If I remove invalidateLater(), the process keeps running. Alternatively, the app works if I use processx handles instead of ps and text files, but this is not sufficient for my use case.

App code

library(callr)
library(ps)
library(shiny)
library(tools)

ui <- fluidPage(
  actionButton("start", "start"),
  actionButton("stop", "stop"),
  textOutput("status")
)

server <- function(input, output, session) {
  file <- tempfile()
  observeEvent(input$start, {
    px <- r_bg(function() Sys.sleep(Inf))
    writeLines(as.character(px$get_pid()), file)
  })
  observeEvent(input$stop, {
    pid <- as.integer(readLines(file))
    if (pid %in% ps_pids()) {
      ps_kill(ps_handle(pid))
    }
  })
  output$status <- renderText({
    invalidateLater(100)
    if (file.exists(file)) {
      pid <- as.integer(readLines(file))
      paste(ifelse(pid %in% ps_pids(), "running", "stopped"), pid)
    }
  })
}

shinyApp(ui = ui, server = server)

Session info

R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] tools     stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
[1] shiny_1.6.0 ps_1.5.0    callr_3.5.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6        magrittr_2.0.1    xtable_1.8-4      R6_2.5.0         
 [5] rlang_0.4.10      fastmap_1.1.0     jquerylib_0.1.3   htmltools_0.5.1.1
 [9] ellipsis_0.3.1    yaml_2.2.1        digest_0.6.27     lifecycle_1.0.0  
[13] processx_3.4.5    later_1.1.0.1     sass_0.3.1        promises_1.2.0.1 
[17] rsconnect_0.8.16  cachem_1.0.4      mime_0.10         compiler_4.0.3   
[21] bslib_0.2.4       jsonlite_1.7.2    httpuv_1.5.5   

EDIT: garbage collection

This is because of garbage-collected processx handles. I can demonstrate this with 2 R sessions. Session 1 creates a background process.

px <- r_bg(function() Sys.sleep(Inf))
px$get_pid()
#> [1] 8252

Session 2 loops to check on the background process spawned from session 1.

library(ps)
while(TRUE) {
  print(8252 %in% ps_pids())
  Sys.sleep(1)
}

Session 2 starts by printing TRUE every second. But the moment I call rm(px); gc() in session 1, session 2 prints FALSE.

I now see that termination on garbage collection is a deliberate feature of processx: https://github.com/r-lib/processx#features. Reasonable for most situations, I guess.

1

There are 1 answers

0
landau On

The process keeps going if I set cleanup to TRUE in callr::r_bg(). Problem solved.