shiny
shiny copied to clipboard
Fix for close ShinySession on on.exit
The onSessionEnd is probably triggered by later
loop and is never trigger on SIGINT
signal. There is a difference between interactive and not interactive R process. In not interactive R process after global onStop callback, the R process is terminated immediately.
The minimal example:
library(shiny)
ui <- fluidPage(
)
server <- function(input, output, session) {
onSessionEnded(function() {
print("close ShinySession session")
})
}
onStop(function() {
# Custom code that was used before this PR.
lapply(shiny:::appsByToken$keys(), function(session) {
session <- shiny:::appsByToken$get(session)
if (isFALSE(session$closed)) session$wsClosed()
})
})
shinyApp(ui, server)
The gif example shows the difference.
If I can do some more in this case or you need more clarification, please let me know.
I think that #2743 may have the same root cause as the issue you're describing.
In your command-line script, if, instead of using this PR, you add this after the call to runApp()
, does the onSessionEnded
callback execute properly?
for (i in 1:10) later::run_now()
@wch I totally agree, it's even better to trigger later loop on stop callback.
It's not really solved #2743 issue because the second runApp is executed before later loop. In my opinion it will be good to add run_now
at the beginning of runApp.
runApp(app)
later::run_now()
runApp(app)
I was to fast, I check twice and it does not execute callback properly.