Resolve the race in shutting down and correctly force flush logs.