Skip to content

fix: the FUSE server field is read under the lock that guards it (#267) - #268

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/mount-server-race
Aug 3, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/mount-server-race

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #267.

Caught by CI's race detector on the run for #265 — a documentation-only PR — so this was live on
main, not introduced by that branch.

The defect

Unmount writes m.server = nil under m.mu. Two places read it without the lock: the goroutine
Mount spawns to serve, and Wait.

WARNING: DATA RACE
Write at 0x00c00046af90 by goroutine 122:
  internal/fuse.(*MountManager).Unmount()      mount.go:322
  internal/adapter.(*Adapter).Stop()           adapter.go:280
Previous read at 0x00c00046af90 by goroutine 234:
  internal/fuse.(*MountManager).Mount.func1()  mount.go:249

Both reads are also nil dereferences, which is what makes this more than a detector report. If
the unmount lands first, m.server.Wait() panics on a background goroutine — unrecoverable, so it
takes the process down together with the mount, unmounting under every open file descriptor.
Wait's if m.server != nil guard does not help: the write can land between the check and the
dereference.

The fix

The serving goroutine waits on the server local already in scope — same server either way, and
outliving the field is that goroutine's whole job. Wait reads the field into a local under m.mu
and waits outside it.

Holding the lock across server.Wait() would also silence the detector and deadlock every real
unmount
, since Unmount needs the same lock to clear the field. That trap is recorded in Wait's
own comment, because it is the change a future reader is most likely to make.

Why the suite did not catch it sooner, and what that means for the test

It reproduces only on Linux and only intermittently: on darwin fs.Mount needs macFUSE, so the
goroutine at mount.go:247 never starts and the racing read is unreachable. The local -race suite
is green while CI went red once in many runs.

A regression test that inherited that property would be worthless. internal/fuse/mount_race_test.go
drives both sides directly and needs no mount — one goroutine writes m.server = nil under m.mu,
another calls Wait() — so it fails deterministically.

Mutation-verified: restoring the unlocked read makes the new test report WARNING: DATA RACE and
fail; the fix makes it pass.

Verification

go build ./... · go vet ./... · gofmt -l . empty · full go test -count=1 -race ./... green ·
coverage gate 34 packages at or above floor · golangci-lint --new-from-merge-base=origin/main → 0
issues.

CI's race detector caught this on the run for #265, a documentation-only pull
request — so it was live on main rather than introduced there.

`Unmount` writes `m.server = nil` under `m.mu`. Two places read it without the
lock: the goroutine `Mount` spawns to serve, and `Wait`.

Both are also nil dereferences, which is what makes it more than a detector
report. If the unmount lands first, `m.server.Wait()` panics on a background
goroutine — unrecoverable, so it takes the process down together with the
mount, unmounting under every open file descriptor. `Wait`'s `if m.server !=
nil` check does not help: the write can land between the check and the
dereference.

The serving goroutine now waits on the `server` local already in scope. It is
the same server either way, and that goroutine's whole job is to outlive the
field. `Wait` reads the field into a local under `m.mu` and waits outside it —
holding the lock across `server.Wait()` would also silence the detector and
would deadlock every real unmount, since `Unmount` needs the same lock to clear
the field.

Why the suite did not catch it sooner: it reproduces only on Linux and only
intermittently. On darwin `fs.Mount` needs macFUSE, so the goroutine never
starts and the racing read is unreachable — the local -race suite is green while
CI went red once in many runs. A regression test inheriting that property would
be worthless, so mount_race_test.go drives both sides directly and needs no
mount: one goroutine writes the field under the lock, another calls Wait.

Mutation-verified: restoring the unlocked read makes the new test report
WARNING: DATA RACE and fail; the fix makes it pass.

Gates: go build, go vet, gofmt -l clean, full `go test -count=1 -race` suite
green, coverage gate 34 packages at or above floor, golangci-lint
--new-from-merge-base=origin/main 0 issues.
Both branches added an [Unreleased] entry; the merge keeps both, ordered
Fixed before Changed as Keep a Changelog specifies.
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.

fuse: MountManager.Wait reads m.server unlocked, racing Unmount's write — and can nil-deref

1 participant