
I believe this was safe before, because this code is only called from the transfer thread, and the condition being protected is just whether the count is zero, not the actual value of the count. However, this isn't performance critical and it's a lot easier to reason about the code if we just hold the lock for this whole section.