Fixing an atomicity violation

In the code above, an atomicity violation may occur between line 80 and 83.

This patch fixes this problem.