From dacc65cfd65948be2caca3e5522d63f517062c4a Mon Sep 17 00:00:00 2001 From: David Sanchez <64162682+dsfaccini@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:27:23 -0500 Subject: [PATCH] Return ENOTCONN from Notifier while no server serves InvalidateInode and InvalidateEntry sent on an unbuffered channel that only the goroutine NewServerWithNotifier starts in ServeOps receives from. Before ServeOps started, or once it had returned, they blocked forever. They now return ENOTCONN while no server wrapped with the Notifier serves. ServeOps waits for the notification in progress before it returns, so that notification is sent on an open connection and every later call returns ENOTCONN. --- notifier.go | 61 ++++++++++++++++++++++++---- notifier_test.go | 102 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+), 8 deletions(-) create mode 100644 notifier_test.go diff --git a/notifier.go b/notifier.go index e9ea38bc..052354d2 100644 --- a/notifier.go +++ b/notifier.go @@ -1,6 +1,8 @@ package fuse import ( + "sync" + "syscall" "unsafe" "github.com/jacobsa/fuse/fuseops" @@ -13,12 +15,21 @@ import ( type Notifier struct { inodeInvalidations chan invalidateInodeCommand dentryInvalidations chan invalidateEntryCommand + + mu sync.Mutex + // stopped is closed while no server wrapped with this Notifier serves. + // + // GUARDED_BY(mu) + stopped chan struct{} } func NewNotifier() *Notifier { + stopped := make(chan struct{}) + close(stopped) return &Notifier{ inodeInvalidations: make(chan invalidateInodeCommand), dentryInvalidations: make(chan invalidateEntryCommand), + stopped: stopped, } } @@ -45,10 +56,18 @@ type invalidateEntryCommand struct { // InvalidateInode blocks until the kernel write completes, and returns the // error from the kernel, if any. ENOSYS indicates that the kernel does not // support inode invalidations. +// +// While no server wrapped with this Notifier serves, before its ServeOps +// starts or once it has returned, InvalidateInode sends nothing and returns +// ENOTCONN. func (n *Notifier) InvalidateInode(inode fuseops.InodeID, offset, length int64) error { done := make(chan error) - n.inodeInvalidations <- invalidateInodeCommand{inode, offset, length, done} - return <-done + select { + case n.inodeInvalidations <- invalidateInodeCommand{inode, offset, length, done}: + return <-done + case <-n.servingStopped(): + return syscall.ENOTCONN + } } // InvalidateEntry notifies to the kernel to invalidate a dentry cache entry. @@ -59,10 +78,25 @@ func (n *Notifier) InvalidateInode(inode fuseops.InodeID, offset, length int64) // InvalidateEntry blocks until the kernel write completes, and returns the // error from the kernel, if any. ENOSYS indicates that the kernel does not // support dentry invalidations. +// +// While no server wrapped with this Notifier serves, before its ServeOps +// starts or once it has returned, InvalidateEntry sends nothing and returns +// ENOTCONN. func (n *Notifier) InvalidateEntry(parent fuseops.InodeID, name string) error { done := make(chan error) - n.dentryInvalidations <- invalidateEntryCommand{parent, name, done} - return <-done + select { + case n.dentryInvalidations <- invalidateEntryCommand{parent, name, done}: + return <-done + case <-n.servingStopped(): + return syscall.ENOTCONN + } +} + +// LOCKS_EXCLUDED(n.mu) +func (n *Notifier) servingStopped() <-chan struct{} { + n.mu.Lock() + defer n.mu.Unlock() + return n.stopped } func serviceInodeInvalidation(c *Connection, inode fuseops.InodeID, offset, length int64) error { @@ -119,12 +153,23 @@ type notifierServer struct { s Server } +// ServeOps sends the Notifier's notifications while s serves. Once s +// returns, it finishes the notification in progress, if any, before it +// returns, so that the connection is still open for it. func (s *notifierServer) ServeOps(c *Connection) { - terminate := make(chan struct{}) - - go s.n.notify(c, terminate) + stopped := make(chan struct{}) + s.n.mu.Lock() + s.n.stopped = stopped + s.n.mu.Unlock() + + notified := make(chan struct{}) + go func() { + defer close(notified) + s.n.notify(c, stopped) + }() s.s.ServeOps(c) - close(terminate) + close(stopped) + <-notified } func NewServerWithNotifier(n *Notifier, s Server) Server { diff --git a/notifier_test.go b/notifier_test.go new file mode 100644 index 00000000..dc7e39c9 --- /dev/null +++ b/notifier_test.go @@ -0,0 +1,102 @@ +// Copyright 2026 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package fuse + +import ( + "errors" + "io" + "os" + "syscall" + "testing" + "unsafe" + + "github.com/jacobsa/fuse/internal/fusekernel" +) + +// blockingServer is a Server whose ServeOps closes started, then returns once +// release is closed. It never reads the connection. +type blockingServer struct { + started chan struct{} + release chan struct{} +} + +func newBlockingServer() blockingServer { + return blockingServer{started: make(chan struct{}), release: make(chan struct{})} +} + +func (s blockingServer) ServeOps(*Connection) { + close(s.started) + <-s.release +} + +func checkNotifierENOTCONN(t *testing.T, n *Notifier, when string) { + t.Helper() + if err := n.InvalidateInode(1, 0, 0); !errors.Is(err, syscall.ENOTCONN) { + t.Errorf("InvalidateInode %s = %v, want ENOTCONN", when, err) + } + if err := n.InvalidateEntry(1, "x"); !errors.Is(err, syscall.ENOTCONN) { + t.Errorf("InvalidateEntry %s = %v, want ENOTCONN", when, err) + } +} + +func TestNotifierReturnsENOTCONNWhileNothingServes(t *testing.T) { + n := NewNotifier() + checkNotifierENOTCONN(t, n, "before ServeOps") + + s := newBlockingServer() + close(s.release) + NewServerWithNotifier(n, s).ServeOps(nil) + checkNotifierENOTCONN(t, n, "after ServeOps returned") +} + +// While the wrapped server serves, the Notifier writes each notification to +// the connection and returns the write's result. +func TestNotifierSendsWhileServing(t *testing.T) { + r, w, err := os.Pipe() + if err != nil { + t.Fatalf("Pipe: %v", err) + } + defer r.Close() + defer w.Close() + + n := NewNotifier() + s := newBlockingServer() + served := make(chan struct{}) + go func() { + NewServerWithNotifier(n, s).ServeOps(&Connection{dev: w}) + close(served) + }() + <-s.started + + if err := n.InvalidateInode(7, 4096, 8192); err != nil { + t.Fatalf("InvalidateInode: %v", err) + } + var msg struct { + Header fusekernel.OutHeader + Body fusekernel.NotifyInvalInodeOut + } + if _, err := io.ReadFull(r, unsafe.Slice((*byte)(unsafe.Pointer(&msg)), unsafe.Sizeof(msg))); err != nil { + t.Fatalf("reading the notification: %v", err) + } + if msg.Header.Error != fusekernel.NotifyCodeInvalInode || msg.Header.Unique != 0 { + t.Errorf("notification header = %+v, want error %d and unique 0", msg.Header, fusekernel.NotifyCodeInvalInode) + } + if want := (fusekernel.NotifyInvalInodeOut{Ino: 7, Off: 4096, Len: 8192}); msg.Body != want { + t.Errorf("notification body = %+v, want %+v", msg.Body, want) + } + + close(s.release) + <-served +}