Fix race during shutdown.

Change-Id: I2f8bf48d92a14f1e5ecde27c1b138734d7653394