Avoid race by locking before reading len(b.all)

[#81524216]