Processes spawned by NonBlockingProcess inherit file descriptors
Opened this issue · 7 comments
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 ...
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()
}
According to the referenced NuProcess issue, this was fixed in NuProcess 1.2 and this project is now using 1.24.
@2m Thanks for the update. BTW - I think that this project is a good candidate for inclusion in alpakka.
Is there any chance to move this project to akka organizations ?
@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.