Fix race condition on exit of trpc threads