Fix reading race conditions and handling exits correctly.