Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ func (c *Connection) handleInterrupt(fuseID uint64) {
cancel()
}

// initInMessage is (*buffer.InMessage).Init. Tests replace it to make a read
// from the device fail.
var initInMessage = (*buffer.InMessage).Init

// Read the next message from the kernel. The message must later be destroyed
// using destroyInMessage.
func (c *Connection) readMessage() (*buffer.InMessage, error) {
Expand All @@ -392,12 +396,17 @@ func (c *Connection) readMessage() (*buffer.InMessage, error) {
// Loop past transient errors.
for {
// Attempt a read.
err := m.Init(c.dev)
err := initInMessage(m, c.dev)

// Special cases:
//
// * ENODEV means fuse has hung up.
//
// * On Linux, ECONNABORTED does too. fuse_dev_do_read returns it when
// the connection is aborted, as an unmount does, after the read
// took a request off the pending list
// (https://github.com/torvalds/linux/blob/v6.12/fs/fuse/dev.c#L1332-L1340).
//
// * EINTR means we should try again. (This seems to happen often on
// OS X, cf. http://golang.org/issue/11180)
//
Expand All @@ -406,6 +415,11 @@ func (c *Connection) readMessage() (*buffer.InMessage, error) {
case syscall.ENODEV:
err = io.EOF

case syscall.ECONNABORTED:
if runtime.GOOS == "linux" {
err = io.EOF
}

case syscall.EINTR:
err = nil
continue
Expand Down
56 changes: 56 additions & 0 deletions connection_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 (
"io"
"os"
"syscall"
"testing"

"github.com/jacobsa/fuse/internal/buffer"
)

// fuse_dev_do_read fails with ECONNABORTED when the connection is aborted
// after the read took a request off the pending list. An unmount aborts the
// connection.
func TestReadOpReturnsEOFWhenTheReadIsAborted(t *testing.T) {
tests := []struct {
name string
errno syscall.Errno
wantEOF bool
}{
{"ENODEV", syscall.ENODEV, true},
{"ECONNABORTED", syscall.ECONNABORTED, true},
{"EIO", syscall.EIO, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var readErr error = &os.PathError{Op: "read", Path: "/dev/fuse", Err: tt.errno}
oldInit := initInMessage
initInMessage = func(*buffer.InMessage, io.Reader) error { return readErr }
t.Cleanup(func() { initInMessage = oldInit })

c := &Connection{inMessageBufSize: buffer.GetPageSize()}
_, _, err := c.ReadOp()
if tt.wantEOF && err != io.EOF {
t.Errorf("ReadOp = %v, want io.EOF", err)
}
if !tt.wantEOF && err != readErr {
t.Errorf("ReadOp = %v, want %v", err, readErr)
}
})
}
}