Add error handling logic

If this failed previously, it would leave an open file descriptor
and pass that to spawned applications without logging anything.
However, test7.9 would fail.