fixing races found by thread sanitizer