_signals.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- test-case-name: twisted.internet.test.test_sigchld -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. This module is used to integrate child process termination into a
  6. reactor event loop. This is a challenging feature to provide because
  7. most platforms indicate process termination via SIGCHLD and do not
  8. provide a way to wait for that signal and arbitrary I/O events at the
  9. same time. The naive implementation involves installing a Python
  10. SIGCHLD handler; unfortunately this leads to other syscalls being
  11. interrupted (whenever SIGCHLD is received) and failing with EINTR
  12. (which almost no one is prepared to handle). This interruption can be
  13. disabled via siginterrupt(2) (or one of the equivalent mechanisms);
  14. however, if the SIGCHLD is delivered by the platform to a non-main
  15. thread (not a common occurrence, but difficult to prove impossible),
  16. the main thread (waiting on select() or another event notification
  17. API) may not wake up leading to an arbitrary delay before the child
  18. termination is noticed.
  19. The basic solution to all these issues involves enabling SA_RESTART
  20. (ie, disabling system call interruption) and registering a C signal
  21. handler which writes a byte to a pipe. The other end of the pipe is
  22. registered with the event loop, allowing it to wake up shortly after
  23. SIGCHLD is received. See L{twisted.internet.posixbase._SIGCHLDWaker}
  24. for the implementation of the event loop side of this solution. The
  25. use of a pipe this way is known as the U{self-pipe
  26. trick<http://cr.yp.to/docs/selfpipe.html>}.
  27. From Python version 2.6, C{signal.siginterrupt} and C{signal.set_wakeup_fd}
  28. provide the necessary C signal handler which writes to the pipe to be
  29. registered with C{SA_RESTART}.
  30. """
  31. from __future__ import division, absolute_import
  32. import signal
  33. def installHandler(fd):
  34. """
  35. Install a signal handler which will write a byte to C{fd} when
  36. I{SIGCHLD} is received.
  37. This is implemented by installing a SIGCHLD handler that does nothing,
  38. setting the I{SIGCHLD} handler as not allowed to interrupt system calls,
  39. and using L{signal.set_wakeup_fd} to do the actual writing.
  40. @param fd: The file descriptor to which to write when I{SIGCHLD} is
  41. received.
  42. @type fd: C{int}
  43. """
  44. if fd == -1:
  45. signal.signal(signal.SIGCHLD, signal.SIG_DFL)
  46. else:
  47. def noopSignalHandler(*args):
  48. pass
  49. signal.signal(signal.SIGCHLD, noopSignalHandler)
  50. signal.siginterrupt(signal.SIGCHLD, False)
  51. return signal.set_wakeup_fd(fd)
  52. def isDefaultHandler():
  53. """
  54. Determine whether the I{SIGCHLD} handler is the default or not.
  55. """
  56. return signal.getsignal(signal.SIGCHLD) == signal.SIG_DFL