[PATCH] Fix race condition when detaching a device