Ticket #2535 (assigned enhancement )

Opened 1 year ago

Last modified 2 months ago

Allow Twisted to work without interfering with subprocess, popen, and other users of SIGCHLD on posix

Reported by: spiv Assigned to: glyph (accepted)
Type: enhancement Priority: normal
Milestone: Component: core
Keywords: Cc:
Author: Branch:

Description

Twisted currently needs to install a SIGCHLD handler for reactor.spawnProcess to work reliably. This is problematic when Twisted is used with other libraries that depend on SIGCHLD (e.g. the standard library's subprocess module depends on SIGCHLD being left as SIG_DFL, otherwise it can get EINTR at unexpected times). Hence the reactor can be run with the installSignalHandlers=False flag, but then reactor.spawnProcess isn't reliable.

However, when run with installSignalHandlers=False Twisted could use a different strategy for notifications of subprocess termination. spawnProcess could first fork a process that keeps a single file descriptor back to the parent open, and then forks the child that the caller of spawnProcess requested. Then first subprocess could simply do:

    pid = os.fork()
    if pid:
       returnVal = wait(pid)
       os.write(fd, str(returnVal))
       os.close(fd)
       os._exit()

i.e. wait for the child process to finish, and then let the original process know the return value and exit. The original process would know that the subprocess was done when it saw the fd get closed, and would know what the return value was.

It's ugly, but it might be better than nothing.

Attachments

poll-for-exit.diff (0.9 kB) - added by glyph 1 year ago.
an very short hack to poll for process exit

Change History

  2007-03-22 01:35:47+00:00 changed by exarkun

Note that as pointed out on #733, it is not necessarily the case that installing a SIGCHLD handler will interfer with the subprocess module (or more commonly popen()).

However, another signal-free solution is just to poll for exited child processes whenever there might be some. This has the advantage of not leaving an extra Python process in memory for every spawnProcess call (not an inconsiderable cost). It has the disadvantage of polling, of course.

  2007-03-23 10:05:04+00:00 changed by glyph

  • attachment poll-for-exit.diff added

an very short hack to poll for process exit

  2007-03-23 10:06:40+00:00 changed by glyph

The local hack that I'm using (because pylucene 1.9 and gcj 4.1 collude to stomp SIGCHLD so badly that signal.getsignal can't even tell that it's been stomped) is in poll-for-exit.diff. It is an implementation of exarkun's suggestion to poll.

  2008-02-01 17:07:26+00:00 changed by exarkun

  • branch deleted
  • author deleted

In addition to #733, see #1997 and #791.

  2008-05-27 20:41:53+00:00 changed by glyph

  • status changed from new to assigned

  2008-05-27 20:42:28+00:00 changed by glyph

  • summary changed from Allow Twisted to work without SIGCHLD on posix to Allow Twisted to work without interfering with subprocess, popen, and other users of SIGCHLD on posix
Note: See TracTickets for help on using tickets.