I think I've found and worked around the relevant problem, which is a race condition.