snapmaker-studio icon indicating copy to clipboard operation
snapmaker-studio copied to clipboard

Mapping store treats a transient file-access error as a corrupt file and sets the live file aside

Open DVOpenLabs opened this issue 1 month ago • 0 comments

Problem

Store._read in backend/snapstudio_core/material_mapping.py treats every OSError the same as a corrupt file:

except FileNotFoundError:
    return [], False
except (OSError, ValueError):
    return [], True      # "damaged"

PermissionError (on Windows, a sharing violation while another process briefly holds the file, such as antivirus, a sync client or another Studio engine) is an OSError, so a transient access failure is reported as confirmed corruption. The damaged flag then makes Store._write(..., keep_damaged=True) move the live file to material-mappings.json.damaged and write a file containing only the one mapping being saved or kept. Every other saved mapping disappears from the live file.

put() and remove() both do rows, damaged = self._read() followed by self._write(rows, keep_damaged=damaged), so any save or forget that overlaps a transient read failure is affected. Plain reads (find, all) just return empty for that call.

Reproduction

This simulates one transient PermissionError on the first read of the mapping file, during an unrelated save (5 saved mappings on disk):

rows on disk before: 5
rows on disk after : 1 | .damaged exists: True
rows in .damaged   : 5

The script patches builtins.open to raise PermissionError(13, "The process cannot access the file because it is being used by another process") once for the mapping file, then calls Store.put(...). (A real-world trigger was also observed with an older build running next to a newer one, where the os.replace of the older process made the newer process's read fail.) No data is lost for good, since the rows are in the .damaged file, but the user's mappings silently stop applying until someone renames the file back.

Why the two cases must stay distinct

  • Confirmed corruption (the file exists, was read in full, and is not valid JSON or has the wrong schema) is stable: reading again gives the same answer, the existing behaviour of setting the file aside and starting fresh is correct, and it is what the current tests cover.
  • A transient access failure says nothing about the content. The file may be perfectly valid. Treating it as corruption destroys valid state, and the failure is invisible because nothing is reported.

A fix should therefore not just retry and then fall back to "damaged". Roughly: only a successful read of bad content may set damaged; an access error should be retried briefly and, if it persists, surface as an error to put/remove (so nothing is written), while read-only callers keep their "empty for this call" behaviour. Which errors count as transient (PermissionError, EACCES/EBUSY/ESTALE, Windows sharing/lock violations) versus permanent (IsADirectoryError, ENOENT on a parent) needs a deliberate decision and tests for both.

Not part of the current PRs

This is pre-existing behaviour, found while reviewing the "Forget saved mapping" work. That work adds an OS file lock around read-modify-write, which removes the race between Studio processes, but it does not change _read, and third-party programs that open the file can still trigger this. It was deliberately left out of those PRs rather than adding a speculative retry.

Acceptance

  • A test where the first read raises PermissionError (and one where it raises for the whole retry window) leaves the live file unchanged and does not create .damaged.
  • Existing damaged/foreign-file tests still pass (test_damaged_or_foreign_file_reads_as_empty_and_is_set_aside_on_write).
  • A save or forget that cannot read the file reports an error rather than succeeding on empty state.

DVOpenLabs avatar Oct 08 '26 16:10 DVOpenLabs