Skip to content

changing_file leaves lifecycle pending when the wrapped write fails #5615

Backlog

Description

@feiiiiii5

Summary

changing_file() in src/specify_cli/integrations/_file_changes.py does not call after_file_change() when the wrapped write or unlink raises. The lifecycle observer adds the path to pending in before_file_change() and removes it only in after_file_change(), so an exception leaves the path pending. _restore_snapshots() skips pending paths, which can prevent rollback from restoring the original bytes of the failed file.

Evidence

At main commit f2010547121b3351e4923fe467205079e4593a32:

@contextmanager
def changing_file(path, *, removal=False):
    before_file_change(path, removal=removal)
    yield
    after_file_change(path)

If yield raises, the second call never happens. In _lifecycle.py, before_file_change() adds path to pending, after_file_change() discards it, and _restore_snapshots() skips pending paths.

A focused observer test can reproduce this without a live agent, database, or network: register an observer that records (path, before, removal), make Path.write_text raise inside changing_file(), and assert that the observer receives both the before and after events. On current main, only the before event is recorded.

Expected behavior

The lifecycle is finalized even when the file operation fails, so rollback can consider the original file and restore it if needed.

Actual behavior

The path remains in pending after the failed operation, and rollback may skip it.

Proposed fix

Move yield into try and call after_file_change(path) in finally. Add a regression test for the exception path.

AI assistance disclosure

Claude Code (model Sonnet 5, default settings, autonomous mode) was used to inspect the lifecycle code, construct this report, and prepare the proposed fix and test. No live service, paid account, or personal data was used.

🤖 Generated with Claude Code

Activity

  1. feiiiiii5 commented on Nov 29, 2026

    @feiiiiii5
    Author

    Retracting this report: its premise is wrong. design/integration.md:396-399 intentionally leaves failed writes pending because their bytes cannot be attributed to the operation; rollback must leave those paths alone. test_lifecycle.py:213-231 covers that behavior. Our PR #5611 made the proposed change and was closed after review for this reason. No further patch will be published.

    AI disclosure: Claude Code (Claude Sonnet 5, default settings, autonomous mode) inspected the code and drafted this report and patch; no human line-by-line review.

  2. added
    triage-must-have
    on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage-must-have

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions