Handle interrupted system calls

Fixes #1332. Please ignore pull request 1353.