Issue2440

classification
Title: Implement os.fork() in Jython
Type: behaviour Severity: normal
Components: Library Versions: Jython 2.7
Milestone:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: teeohhem, zyasoft
Priority: Keywords:

Created on 2015-12-08.16:26:52 by teeohhem, last changed 2015-12-16.19:11:03 by zyasoft.

Messages
msg10517 (view) Author: Tom Alexander (teeohhem) Date: 2015-12-08.16:26:52
Jython should implement os.fork() for even better Twisted support. Now that Jython 2.7.1 will include os.pipe(), many Twisted tests and code branches will now fail because it gets further in the process where os.fork() is eventually called. Perhaps we should consider using java subprocesses for this behavior.
msg10525 (view) Author: Jim Baker (zyasoft) Date: 2015-12-09.01:48:29
This has been a major wish list item for a while on the JVM; but see this in the corresponding JRuby project:

"JRuby doesn't implement fork() on any platform, including those where fork() is available in MRI. This is due to the fact that most JVMs cannot be safely forked."

https://github.com/jruby/jruby/wiki/DifferencesBetweenMriAndJruby#fork-is-not-implemented

For such purposes, Jython should be considered to be much like Python on Windows - no fork available. Subprocess is often, but not always, the same - think of daemonization, inherited file handles, etc.
msg10539 (view) Author: Tom Alexander (teeohhem) Date: 2015-12-16.16:18:29
Yeah I can certainly understand the reasoning for not implementing this. Unfortunately, this has a major impact on compatibility with Twisted and running a process as a service or daemon. Any suggestions on where to go from here?
msg10540 (view) Author: Jim Baker (zyasoft) Date: 2015-12-16.19:11:02
Besides some minor testing usage, Twisted uses `os.fork` for two purposes:

1. Daemonization support. For Java, we can do this a number of ways:

   a. Cassandra, as one example, simply closes stderr and stdout; see https://github.com/apache/cassandra/blob/trunk/src/java/org/apache/cassandra/service/CassandraDaemon.java#L560

            if (System.getProperty("cassandra-foreground") == null)
            {
                System.out.close();
                System.err.close();
            }

Note this doesn't actually daemonize anything; to do that, you need to use something like Debian's `start-stop-daemon` command, which is used by the DataStax init.d scripts.

    b. Various other good starting points exist for Linux. I found this blog post helpful: http://www.mikeperham.com/2014/09/22/dont-daemonize-your-daemons/, which then points at the systemd man pages. Now that major distros have standardized on it, systemd especially works well and systemd can manage the complexity of daemonization. In other words, no worries about its former competitor, upstart.

    c. Apache Commons, specifically http://commons.apache.org/proper/commons-daemon/index.html, using either jsvc for Unix-like systems or procrun for Windows to get the equivalent idea on that architecture. Tomcat uses this approach; see https://tomcat.apache.org/tomcat-8.0-doc/setup.html

2. Process building in `twisted.internet.process`. So this is really akin to what `subprocess` does, but looks like with more control. Perhaps it's something we can get at a Jython-specific equivalent using Java Native Runtime with https://github.com/jnr/jnr-process, so long as we are on Unix-like systems.

Most likely the resolution of this bug is docs on the Jython wiki re daemonization; and possibly enhanced support for process building as part of a Jython-specific reactor.
History
Date User Action Args
2015-12-16 19:11:03zyasoftsetmessages: + msg10540
2015-12-16 16:18:30teeohhemsetmessages: + msg10539
2015-12-09 01:48:30zyasoftsetnosy: + zyasoft
messages: + msg10525
2015-12-08 16:26:52teeohhemcreate