Skip to content

fix(fs): preserve localFileInodes invariants on failed local file upload - #5277

Draft
saisagar1998 wants to merge 1 commit into
masterfrom
fix/issue-5272-local-file-inodes
Draft

saisagar1998 wants to merge 1 commit into
masterfrom
fix/issue-5272-local-file-inodes

Conversation

@saisagar1998

Copy link
Copy Markdown
Member

Description

When uploading a newly created local file fails in flushFile, syncFile, or createLocalFile, fs.localFileInodes could become out of sync with the inode state:

  • Removing a still-linked local inode (IsLocal() == true, IsUnlinked() == false) from fs.localFileInodes caused checkInvariantsForLocalFileInodes() to panic with localFileInodes mismatch under --debug_invariants.
  • Unguarded delete(fs.localFileInodes, f.Name()) calls could evict a newer local file inode with the same name.
  • In createLocalFile, errors in createStreamingWriteHandlerAndSyncOrTempWriter left the minted inode in fs.inodes and leaked the lock/lookup count in the defer.

Changes:

  • flushFile: only unlink and remove from fs.localFileInodes when f.IsLocal() is true, calling f.Unlink() under f's lock before taking fs.mu and guarding deletion with if fs.localFileInodes[f.Name()] == f.
  • syncFile: remove the unguarded delete(fs.localFileInodes, f.Name()) on error instead of calling f.Unlink(). Leaving the inode linked keeps invariants valid, preserves retryability for transient fsync errors, and ensures subsequent FlushFile on close still reports upload errors (such as ESTALE) rather than returning nil for an unlinked inode.
  • promoteToGenerationBacked: guard delete(fs.localFileInodes, name) with if fs.localFileInodes[name] == f.
  • createLocalFile: on failure in createStreamingWriteHandlerAndSyncOrTempWriter, unlink, remove from fs.localFileInodes, and dispose the minted inode via unlockAndDecrementLookupCount; also ensure fs.mu.Unlock() always runs and child is only locked/incremented when err == nil.

Link to the issue in case of a bug fix.

Fixes #5272 (b/587595321)

Testing details

  1. Manual - NA
  2. Unit tests - Added TestLocalFile_UploadFailurePreservesInvariants (staged_writes, streaming_writes, zonal_streaming_writes) in internal/fs/fs_internal_test.go with locker.EnableInvariantsCheck().
    • go test -race -v -count=1 ./internal/fs -run TestLocalFile_UploadFailurePreservesInvariants (pass)
    • go test -count=1 ./internal/fs -run "TestStaleFileHandle|TestLocalFileTest" (pass)
    • go vet ./internal/fs/... (pass)
  3. Integration tests - NA

Any backward incompatible change? If so, please explain.

No.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors error handling and cleanup logic for local files in the filesystem, particularly when sync or flush operations fail. It ensures that local file inodes are correctly unlinked and removed from localFileInodes only if they match the active inode, preventing accidental eviction of newer files with the same name. Additionally, new unit tests are added to verify that upload failures preserve filesystem invariants. As there are no review comments, no further feedback is provided.

@codecov

codecov Bot commented Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.23810% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 85.48%. Comparing base (dd019ff) to head (347eb1b).

Files with missing lines Patch % Lines
internal/fs/fs.go 95.23% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5277      +/-   ##
==========================================
+ Coverage   85.13%   85.48%   +0.34%     
==========================================
  Files         181      181              
  Lines       19875    19879       +4     
==========================================
+ Hits        16921    16993      +72     
+ Misses       2953     2885      -68     
  Partials        1        1              
Flag Coverage Δ
unittests 85.48% <95.23%> (+0.34%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant