Close file as soon as possible to avoid potential readlocks being kept too long. Might be enough for #415