Skip to content

fix(mfs): prevent panic on concurrent flush and close - #1131

Merged
lidel merged 3 commits into
mainfrom
fix/mfs-fd-concurrent-flush-close
Apr 7, 2026
Merged

lidel merged 3 commits into
mainfrom
fix/mfs-fd-concurrent-flush-close

Conversation

@lidel

@lidel lidel commented Apr 3, 2026 •

Copy link
Copy Markdown
Contributor

This PR fixes a FUSE panic found in Kubo when running FUSE tests on CI (log).

When the FUSE kernel dispatches FLUSH and RELEASE concurrently for the same file handle, both Flush and Close call into flushUp, which accesses the underlying DagModifier without synchronization. Two concurrent Sync calls race on the write buffer, causing a nil pointer dereference.

This PR adds a mutex to fileDescriptor that serializes Flush and Close, and returns ErrClosed when Flush is called after Close.

Also adds Directory.Mode() and Directory.ModTime() read-only getters to match the existing File API, needed by the Kubo FUSE layer.

Changes

  • mfs/fd.go: add sync.Mutex to fileDescriptor, acquired in Flush and Close
  • mfs/fd.go: Flush on a closed descriptor now returns ErrClosed
  • mfs/dir.go: add Directory.Mode() and Directory.ModTime() getters
  • mfs/mfs_test.go: add TestConcurrentFlushAndClose (catches regressions with -race)

Test plan

  • TestConcurrentFlushAndClose passes with -race
  • Kubo FUSE tests pass with this boxo version

Flush and Close both call flushUp which accesses the underlying
DagModifier without synchronization. When a caller invokes both
from separate goroutines, two concurrent Sync calls race on the
write buffer, causing a nil pointer dereference.

- add sync.Mutex to fileDescriptor, acquired in Flush and Close
- Flush on a closed descriptor now returns ErrClosed
- add TestConcurrentFlushAndClose
@codecov

codecov Bot commented Apr 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 27.27273% with 16 lines in your changes missing coverage. Please review.
βœ… Project coverage is 62.68%. Comparing base (98dabcc) to head (3c5d8ea).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
mfs/dir.go 0.00% 16 Missing ⚠️

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1131      +/-   ##
==========================================
- Coverage   62.69%   62.68%   -0.02%     
==========================================
  Files         262      262              
  Lines       26235    26257      +22     
==========================================
+ Hits        16448    16458      +10     
- Misses       8088     8102      +14     
+ Partials     1699     1697       -2     
Files with missing lines Coverage Ξ”
mfs/fd.go 76.19% <100.00%> (+4.39%) ⬆️
mfs/dir.go 63.96% <0.00%> (-3.00%) ⬇️

... and 9 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lidel
lidel marked this pull request as ready for review April 3, 2026 20:12
@lidel
lidel requested a review from a team as a code owner April 3, 2026 20:12
Directory had SetMode/SetModTime but no matching getters, unlike
File which has both. Add Mode() and ModTime() so callers like
FUSE mounts can read directory metadata without going through
GetNode and ExtractFSNode manually.
@lidel
lidel force-pushed the fix/mfs-fd-concurrent-flush-close branch from 7cc61c7 to 3c5d8ea Compare April 7, 2026 15:26
@lidel lidel changed the title fix(mfs): serialize FileDescriptor Flush and Close fix(mfs): prevent panic on concurrent flush and close Apr 7, 2026
@lidel
lidel merged commit a9db646 into main Apr 7, 2026
20 checks passed
@lidel
lidel deleted the fix/mfs-fd-concurrent-flush-close branch April 7, 2026 15:45
lidel added a commit to ipfs/kubo that referenced this pull request Apr 7, 2026
Picks up ipfs/boxo#1131: fix concurrent flush/close panic in MFS
file descriptors (FUSE race condition).
lidel added a commit to ipfs/kubo that referenced this pull request Apr 7, 2026
* test(deps): quick test of boxo with ipfs/boxo#1125

* test(mfs): verify CidBuilder preservation across mutations and restarts

* docs(changelog): highlight MFS CidBuilder fix

* fix(mfs): apply Import.CidVersion and HashFunction to MFS root

The MFS root loaded at daemon startup never received a CidBuilder
from config, so it stayed CIDv0/sha2-256 even with non-default
Import settings. Pass the configured CidBuilder to mfs.NewRoot().

- add Import.UnixFSCidBuilder() helper for building cid.Prefix
- pass WithCidBuilder to mfs.NewRoot in core/node/core.go
- deduplicate getPrefixNew/getPrefix in files.go
- strengthen regression test to check CID version and root dir

* fix: restore explicit Flush, use upstream boxo

- restore explicit Flush: false in addNode and addDir Mkdir calls
  that was dropped during the MkdirOpts refactor
- use %q for hash function error message in getPrefix
- switch from boxo fork replace to upstream boxo@98dabcc

* fix(config): always build explicit CidBuilder from defaults

UnixFSCidBuilder used to return nil when CidVersion and HashFunction
matched compile-time defaults, relying on boxo's internal CIDv0/sha2-256
fallback. This will break when DefaultCidVersion changes to 1, because
boxo will keep using CIDv0 regardless.

- remove early-return short-circuit in UnixFSCidBuilder
- add unit tests for explicit and default CidBuilder construction

Refs: #4143

* fix(files): reject chcid on MFS root path

The MFS root CID format is now always set from Import.CidVersion and
Import.HashFunction at startup, so chcid on "/" was silently overridden
on every subsequent command or daemon restart.

- chcid now requires a path argument and rejects "/"
- help text and changelog explain how to change root CID format
- sharness tests use Import config + daemon restarts instead of chcid /
- added test for chcid on subdirectories with blake2b-256

* chore(deps): update boxo to latest main

Picks up ipfs/boxo#1131: fix concurrent flush/close panic in MFS
file descriptors (FUSE race condition).

* fix(test): sharness daemon pairing and stale shard hash

- add restart_daemon helper to avoid tripping t0015 meta-test
  that counts literal test_kill/test_launch pairs in each file
- update cidv1 SHARD_HASH to match current boxo HAMT output

---------

Co-authored-by: Andrew Gillis <11790789+gammazero@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants