Fix infinite recursion when a file is invalid (corrupted, empty, or mid-write)