Fix a number of threading bugs, both in the test suite and in the timeout manager.