Protect against file being closed before operations