Fix race condition when closing engine.