# Spawned processes not being shut down when running multiple chains in Rstudio

**URL:** https://discourse.mc-stan.org/t/spawned-processes-not-being-shut-down-when-running-multiple-chains-in-rstudio/10381
**Category:** RStan
**Created:** [August 19, 2019, 3:39pm UTC](https://discourse.mc-stan.org/t/spawned-processes-not-being-shut-down-when-running-multiple-chains-in-rstudio/10381 "2019-08-19T15:39:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![andrewgelman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrewgelman/32/1577_2.png) [@andrewgelman](https://discourse.mc-stan.org/u/andrewgelman)
#### Post date: [August 19, 2019, 3:39pm UTC](https://discourse.mc-stan.org/t/spawned-processes-not-being-shut-down-when-running-multiple-chains-in-rstudio/10381/1 "2019-08-19T15:39:09Z")

</div>

This is a problem we had awhile ago, then I thought it got fixed, but it’s been happening again.

Here’s what happened: I ran Stan with 4 chains in Rstudio. It was running and I stopped it in the middle. But the 4 chains kept running in the background. I had to go to the Activity Monitor and shut them down one at a time by hand.

---

<div class="post-metadata">

### Author: ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)
#### Post date: [August 19, 2019, 6:12pm UTC](https://discourse.mc-stan.org/t/spawned-processes-not-being-shut-down-when-running-multiple-chains-in-rstudio/10381/2 "2019-08-19T18:12:01Z")

</div>

I’ve encountered this occasionally as well, which is one of the reasons I made [ezStan](https://github.com/mike-lawrence/ezStan), which has a `kill_stan()` function for killing wayward chains. Note, however, that all `kill_stan()` does is run the unix command `killall R`, so it will kill any R sessions being run from the command line.

BTW, you might like the alternative interface that `start_stan()` provides whereby running chains doesn’t lock up your primary R session; you can check in on sampling progress any time by running `watch_stan()`, which you can either kill with ctrl-c (or clicking stop in the Console UI) or leave running until sampling is done (with an alert sound if you have the beepr package installed). In addition to slightly prettier progress indicators, `watch_stan()` shows if divergences have been encountered and gives you the option to automatically run `kill_stan()` if this happens. I’m working on adding more pre- and post-warmup diagnostics to compute & present during sampling as well.

---

<div class="post-metadata">

### Author: ![aornugent](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/aornugent/32/1142_2.png) [@aornugent](https://discourse.mc-stan.org/u/aornugent)
#### Post date: [August 19, 2019, 9:49pm UTC](https://discourse.mc-stan.org/t/spawned-processes-not-being-shut-down-when-running-multiple-chains-in-rstudio/10381/3 "2019-08-19T21:49:12Z")

</div>

I think this was addressed in a Rstudio update last year: [RStudio 1.2 Preview: Stan - Posit](https://blog.rstudio.com/2018/10/16/rstudio-1-2-preview-stan/)

> Worker Interruption  
> One aspect that had previously made working with Stan in RStudio frustrating was the inability to interrupt parallel Stan workers. This implied that attempts to fit a computationally expensive model could not be interrupted, and the only remedy previously was to restart the IDE or forcefully shut down the workers through another mechanism.

> We’re very happy to share that this limitation has been lifted with RStudio v1.2. Now, when fitting a Stan model with parallel workers, you can interrupt the workers as you would any regular R code – either use the Escape key, or press the Stop button in the Console pane.

---

<div class="post-metadata">

### Author: ![andrewgelman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrewgelman/32/1577_2.png) [@andrewgelman](https://discourse.mc-stan.org/u/andrewgelman)
#### Post date: [August 20, 2019, 2:34pm UTC](https://discourse.mc-stan.org/t/spawned-processes-not-being-shut-down-when-running-multiple-chains-in-rstudio/10381/4 "2019-08-20T14:34:55Z")

</div>

Yes, I remember that from last year and I think the problem was fixed for awhile. But now it’s returned.
