The balancer registers a shutdown task when it is created, but this happens after the mongod process is multithreaded, so it's possible for the server to be shutting down when the task is registered, which triggers this invariant.
Instead, we should probably check if the balancer exists in shutdownTask() defined in db.cpp and shut it down if it does, similar to what's done for other components.