Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching / blocking SIGINT during system call

I've written a web crawler that I'd like to be able to stop via the keyboard. I don't want the program to die when I interrupt it; it needs to flush its data to disk first. I also don't want to catch KeyboardInterruptedException, because the persistent data could be in an inconsistent state.

My current solution is to define a signal handler that catches SIGINT and sets a flag; each iteration of the main loop checks this flag before processing the next url.

However, I've found that if the system happens to be executing socket.recv() when I send the interrupt, I get this:

^C
Interrupted; stopping...  // indicates my interrupt handler ran
Traceback (most recent call last):
  File "crawler_test.py", line 154, in <module>
    main()
  ...
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 397, in readline
    data = recv(1)
socket.error: [Errno 4] Interrupted system call

and the process exits completely. Why does this happen? Is there a way I can prevent the interrupt from affecting the system call?

like image 533
danben Avatar asked Jun 10 '10 16:06

danben


People also ask

Can you interrupt a system call?

System calls are not interrupts because they are not triggered asynchronously by the hardware. A process continues to execute its code stream in a system call, but not in an interrupt.

Which Sigaction flag is used to prevent a system call from being interrupted?

You can set sa_mask in your sigaction call to block certain signals while a particular signal handler runs. This way, the signal handler can run without being interrupted itself by signals.

What does interrupted system call mean?

Interruption of a system call by a signal handler occurs only in the case of various blocking system calls, and happens when the system call is interrupted by a signal handler that was explicitly established by the programmer.


2 Answers

socket.recv() calls the underlying POSIX-compliant recv function in the C layer, which, in turn, will return an error code EINTR when the process receives a SIGINT while waiting for incoming data in recv(). This error code can be used on the C side (if you were programming in C) to detect that recv() returned not because there is more data available on the socket but because the process received a SIGINT. Anyway, this error code is turned into an exception by Python, and since it is never caught, it terminates your application with the traceback you see. The solution is simply to catch socket.error, check the error code and if it is equal to errno.EINTR, ignore the exception silently. Something like this:

import errno

try:
    # do something
    result = conn.recv(bufsize)
except socket.error as (code, msg):
    if code != errno.EINTR:
        raise
like image 86
Tamás Avatar answered Sep 28 '22 02:09

Tamás


If you don't want your socket call to be interrupted disable the interrupt behavior after you set the signal handler.

signal.signal(<your signal here>, <your signal handler function here>)
signal.siginterrupt(<your signal here>, False)

In the signal handling function set some flag, e.g. a threading.Event() and then check that flag in your main processing function and terminate your crawler gracefully.

Background info here:

like image 36
Eugen Avatar answered Sep 28 '22 03:09

Eugen