Skip to content

Keep osfs file contents intact when ReplaceFile or CreateFile writes fail #2992

Todo

Description

@blkt

Workspace.ReplaceFile in internal/adapter/osfs/osfs.go checks the version under the path lock, then writes through writeResolved (L1359), which calls os.Root.WriteFile on the target (L1255, relaxed path L1261). WriteFile truncates the file before writing, so a failure partway through (ENOSPC, EDQUOT, an I/O error) returns an error to the agent but leaves the target truncated or partial: the old content is gone, and the version the agent holds no longer matches anything it can recover. The adapter-public Workspace.Write (L1023) shares the same path.

createResolved has a related problem: it opens the target with O_CREATE|O_EXCL (L1277) and, when file.Write (L1284) or file.Close (L1288) fails, returns the error without removing the file. A retried CreateFile (or CopyFile, which goes through it) then fails with fs.ErrExist against an empty or partial file the agent never successfully created.

Storage failures are expected on the Kubernetes execution path (see when the storage fails), and #2698 maps ENOSPC to a tool error. Reporting the error is correct; losing or leaving behind data is not.

The usual remedy is one option: write to a temporary file in the same directory, fsync it, and rename it over the target (for create, a no-replace link or rename, or removing the file on failure). Whatever the approach, ReplaceFile must keep its CAS semantics (compare and write stay atomic under the path lock, VersionMismatchError and fs.ErrNotExist unchanged), and CreateFile must stay exclusive against non-cooperating creators.

Acceptance criteria

  • A failed ReplaceFile write leaves the target with its previous content and version; a test injects a write failure and asserts the old bytes are still readable.
  • A failed CreateFile write leaves no file behind; a test injects a write failure and asserts a retried create succeeds.
  • Workspace.Write gets the same guarantee as ReplaceFile.
  • Existing file mode, relaxed-write confinement, and symlink handling are unchanged.
  • No temporary files are left in the workspace after success or failure.
  • task lint && task test:race pass, and nothing under engine/ changes.

Size

S. Two write helpers in one file, plus fault-injection tests.

Activity

  1. ningsiyuan1024 commented on Nov 26, 2026

    @ningsiyuan1024

    I'd like to take this one. I'll start with a failing Workspace test that checks two things after an interrupted write: the previous contents and version survive a failed replacement, and a failed create can be retried at the same path. Could you assign this to me?

  2. blkt commented on Nov 26, 2026

    @blkt
    ContributorAuthor

    @ningsiyuan1024 please do, and ping me in the PR when you've got one! 🚀

  3. added
    bug
    on Nov 26, 2026
  4. ningsiyuan1024 commented on Nov 28, 2026

    @ningsiyuan1024

    I tested Workspace.CreateFile on the same local SSHFS 3.7.1 mount with hard links disabled. On a free path, upstream 029c818e created the complete file. My staged hard-link candidate returned ENOSYS and left the target absent. Both builds rejected an occupied path and preserved its contents. renameat2(RENAME_NOREPLACE) also returned EINVAL with a free destination.

    Would it be acceptable for CreateFile to return a clear unsupported-filesystem error before creating the target on mounts where safe publication and safe cleanup are both unavailable? Direct exclusive create works there today, but checking file identity before deleting by path can still remove another process's replacement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions