typelevel / typelevel/fs2

Process[IO] that spawns child processes might not respect cancellation

Open
#3,693 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Scala
Stars
2.5k
Forks
636
Avg merge
2d 4h
Merged PRs (30d)
7

Description

The jvm implementation of Process[IO] in linux might not respect cancellation if the main process does not itself kill the child processes.

A simple example, this should not respect the cancel and take +10 seconds to finish:

//> using scala "2.13.16"
//> using dep "co.fs2::fs2-io:3.12.2"
import cats.effect.{IO, IOApp}
import fs2.io.process.ProcessBuilder
import scala.concurrent.duration._
object Main extends IOApp.Simple {
  def run: IO[Unit] =
    ProcessBuilder("bash", List("-c", "sleep 10 & sleep 10")).spawn[IO].use { proc =>
      proc.stdout.merge(proc.stderr).compile.drain.start.flatTap(_ => IO.sleep(200.millis)).flatMap(_.cancel)
    }
}

This one will stop immediately:

//> using scala "2.13.16"
//> using dep "co.fs2::fs2-io:3.12.2"
import cats.effect.{IO, IOApp}
import fs2.io.process.ProcessBuilder
import scala.concurrent.duration._
object Main extends IOApp.Simple {
  def run: IO[Unit] =
    ProcessBuilder("sleep", List("10")).spawn[IO].use { proc =>
      proc.stdout.merge(proc.stderr).compile.drain.start.flatMap(_.cancel)
    }
}

⚠️ what follows includes LLM "facts", words are mine ⚠️

To fix the real case where this happened to me, I ended up using a jdk Process and calling destroyForcibly on all descendants. This apparently is subject to a race condition where processes could be spawned while the loop is happening.
It seems the correct approach (in linux) would be to use process groups, then fs2 would simply kill the whole group. This is apparently not supported by the jvm, so it would require jni.

At the very least I think this needs a mention in the docs, I will PR that soon unless someone proposes a solution.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at fs2.io.process.ProcessBuilder.spawn on the Linux/JVM implementation and reproduce the two cancellation examples from the issue. Compare cancellation when spawning sleep directly with the bash command that creates child processes. Done means determining whether child processes can be terminated reliably, or documenting the limitation and supported behavior if no safe implementation is available.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.