1

I have the following program:

import socket
import sys
import threading
import signal

class serve(threading.Thread):
    def __init__(self):
        super(serve, self).__init__()
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.host = ''
        self.port = int(sys.argv[1])
    def run(self):
            self.s.bind((self.host, self.port))
            self.s.listen(1)
            conn, addr = self.s.accept()
            # Call blocks in the following recv
            data = conn.recv(1000000)
            conn.close()
            self.s.close()

def handler(signum, frame):
    print "I am the handler: "

signal.signal(signal.SIGHUP, handler)

background = serve()
background.start()
background.join()

There is a client program that connects to this but does not send any data. The problem is when a SIGHUP is sent, and "Interrupted System call" exception is being thrown. Any idea why? It is happening in python 2.6+ and on FreeBSD. I suspect it is related to http://bugs.python.org/issue1975.

amit
  • 10,612
  • 11
  • 61
  • 60

1 Answers1

1

If a system call is executing when a signal arrives, the system call is interrupted. I believe this is in part to prevent escalation attacks and in part to keep the process in a consistent state when the signal handler is invoked. It also would allow you to wake up a process that's hung on a system call.

To instead restart system calls after a signal is handled, use signal.siginterrupt after you set a signal handler:

signal.signal(signal.SIGHUP, handler)
signal.siginterrupt(signal.SIGHUP, false)
outis
  • 75,655
  • 22
  • 151
  • 221