Fix major bugs involving file descriptor leaks and unhandled signals