Fix potential race condition.

Found when I noticed that the unit tests segfaulted every once in a while.

Test: Ran all unit tests 1000 times. The crash usually happens before 100 runs.
Change-Id: I1c8d2e3637400dc80f273f3677f4c94b0dbeac9d
1 file changed