typesafehub / akka-contrib-extra

ConductR Akka contributions
Other
9 stars 16 forks source link

Processes spawned by NonBlockingProcess inherit file descriptors #70

Open longshorej opened 7 years ago

longshorej commented 7 years ago

On Linux, processes spawned by NonBlockingProcess inherit the JVM's open file descriptors. The underlying cause it due to this NuProcess issue.

The following can be used as a work-around -- spawn bash to run these commands, and then exec the command you wished to spawn:

if [ -d /proc/$$/fd/ ]; then
  for descriptor_path in /proc/$$/fd/*; do
      descriptor="$(basename "$descriptor_path")"
      # Don't close stdin/stderr/stdout (-gt 2)
      if [ $descriptor -gt 2 ]; then
        exec {descriptor}<&-
      fi
  done
fi

exec command arg1 arg2 ...
huntc commented 7 years ago

Here's a complete example for Scala (uses the jna library for native callouts):

package example

import com.sun.jna.{ Native, NativeLibrary }
import java.nio.file.{ Files, NoSuchFileException, Paths }

object Hello extends App {
  Native.register(NativeLibrary.getProcess());

  @native def close(fd: Int): Int

  @native def getpid(): Int

  def closeInheritedFds(): Unit = {
    val pid = getpid()
    try {
      val dirStream = Files.newDirectoryStream(Paths.get(s"/proc/$pid/fd", "\\d*"))
      try {
        dirStream.forEach { path =>
          val fd = path.getFileName.toString.toInt
          if (fd > 2) close(fd) // Don't close stdin/stderr/stdout (> 2)
        }
      } finally {
        dirStream.close()
      }
    } catch {
      case _: NoSuchFileException =>
    }
  }

  closeInheritedFds()
}
dpennell commented 5 years ago

According to the referenced NuProcess issue, this was fixed in NuProcess 1.2 and this project is now using 1.24.

2m commented 5 years ago

It was fixed in https://github.com/typesafehub/akka-contrib-extra/commit/7ca1ec0c6ce496fa902a57199b50b95e580f8636 but it has not yet been released.

dpennell commented 5 years ago

@2m Thanks for the update. BTW - I think that this project is a good candidate for inclusion in alpakka.

He-Pin commented 5 years ago

Is there any chance to move this project to akka organizations ?

raboof commented 5 years ago

@dpennell @hepin1989 I think we want to eventually phase out this library and move individual pieces either into Akka, Alpakka, or 'personal' repositories of maintainers outside of the Akka organization. Probably best to create issues about individual pieces in the respective repositories. I'll add a note to the README.

dpennell commented 5 years ago

@raboof - My comment was only about the process package. I'll open a ticket in the Alpakka project.