Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Processes spawned by NonBlockingProcess inherit file descriptors #70

Open
longshorej opened this issue Sep 14, 2017 · 7 comments
Open

Comments

@longshorej
Copy link
Contributor

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
Copy link
Contributor

huntc commented Sep 19, 2017

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
Copy link

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

@2m
Copy link
Contributor

2m commented Nov 30, 2018

It was fixed in 7ca1ec0 but it has not yet been released.

@dpennell
Copy link

dpennell commented Dec 2, 2018

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

@He-Pin
Copy link

He-Pin commented Dec 2, 2018

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

@raboof
Copy link
Contributor

raboof commented Dec 4, 2018

@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
Copy link

dpennell commented Dec 4, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants