fix potential race condition in detached threads