bpo-31976: Fix race condition when flushing a file is slow. (#4331)

4 files changed