Handle interrupted system calls

Fixes #1322. Please ignore pull request 1353.

(Fixded issue number)