fix potential race, fix test case