Fix races

This fixes several races found by running the race detector on CI.
6d00b1afd2d6956a4f25c2acd9c8420162a4a06c