Fix an assertion failure caused by double close on exit