Fix race condition in client teardown