The main problem is that sudo cannot be killed when it prompts password, as it's run by root, so the correct way out is to close the write fd.