Fix race condition on daemon shutdown (#20543) Upstream-commit: 2af84d8875045f853210dcdab2548daaad70856f Component: engine