Fix exception when trying to open nonexistent files in read mode.
Fix race condition.