Fix a bunch of race conditions when there are short-lived threads.