Skip to content

Commit 1e76f5a

Browse files
committed
Fix bug in umount logic, causing long delays.
Signed-off-by: Thomas Hallgren <[email protected]>
1 parent 998812b commit 1e76f5a

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

pkg/fs/ftp.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package fs
33

44
import (
55
"bytes"
6-
"context"
76
"errors"
87
"io"
98
"io/fs"
@@ -30,9 +29,6 @@ type fuseImpl struct {
3029
// connPool is the pool of control connections to the remote FTP server.
3130
pool connPool
3231

33-
// cancel the GC loop
34-
cancel context.CancelFunc
35-
3632
// Mutex protects nextHandle, current, and shuttingDown
3733
sync.RWMutex
3834

@@ -114,10 +110,8 @@ type FTPClient interface {
114110
// NewFTPClient returns an implementation of the fuse.FileSystemInterface that is backed by
115111
// an FTP server connection tp the address. The dir parameter is the directory that the
116112
// FTP server changes to when connecting.
117-
func NewFTPClient(ctx context.Context, addr netip.AddrPort, dir string, ro bool, readTimeout time.Duration) (FTPClient, error) {
118-
ctx, cancel := context.WithCancel(ctx)
113+
func NewFTPClient(done <-chan struct{}, addr netip.AddrPort, dir string, ro bool, readTimeout time.Duration) (FTPClient, error) {
119114
f := &fuseImpl{
120-
cancel: cancel,
121115
current: make(map[uint64]*info),
122116
readOnly: ro,
123117
pool: connPool{
@@ -126,10 +120,11 @@ func NewFTPClient(ctx context.Context, addr netip.AddrPort, dir string, ro bool,
126120
},
127121
}
128122
go func() {
123+
defer f.Destroy()
129124
ticker := time.NewTicker(stalePeriod)
130125
for {
131126
select {
132-
case <-ctx.Done():
127+
case <-done:
133128
return
134129
case <-ticker.C:
135130
f.pool.tidy()
@@ -138,7 +133,6 @@ func NewFTPClient(ctx context.Context, addr netip.AddrPort, dir string, ro bool,
138133
}()
139134

140135
if err := f.pool.setAddr(addr); err != nil {
141-
cancel()
142136
return nil, err
143137
}
144138
return f, nil
@@ -191,7 +185,6 @@ func (f *fuseImpl) Destroy() {
191185
}
192186
wg.Wait()
193187
f.pool.quit()
194-
f.cancel()
195188
}
196189

197190
// Flush is a noop in this implementation

pkg/fs/ftp_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func startFUSEHost(t *testing.T, ctx context.Context, port uint16, dir string, r
139139
// Start the client
140140
dir = filepath.Join(dir, "mount")
141141
require.NoError(t, os.Mkdir(dir, 0755))
142-
fsh, err := NewFTPClient(ctx, netip.MustParseAddrPort(fmt.Sprintf("127.0.0.1:%d", port)), remoteDir, readOnly, 60*time.Second)
142+
fsh, err := NewFTPClient(ctx.Done(), netip.MustParseAddrPort(fmt.Sprintf("127.0.0.1:%d", port)), remoteDir, readOnly, 60*time.Second)
143143
require.NoError(t, err)
144144
mp := dir
145145
if runtime.GOOS == "windows" {
@@ -153,7 +153,7 @@ func startFUSEHost(t *testing.T, ctx context.Context, port uint16, dir string, r
153153

154154
func TestConnectFailure(t *testing.T) {
155155
ctx := testContext(t)
156-
_, err := NewFTPClient(ctx, netip.MustParseAddrPort("198.51.100.32:21"), "", false, time.Second)
156+
_, err := NewFTPClient(ctx.Done(), netip.MustParseAddrPort("198.51.100.32:21"), "", false, time.Second)
157157
require.Error(t, err)
158158
}
159159

pkg/fs/fuse.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ func (fh *FuseHost) Start(ctx context.Context, startTimeout time.Duration) error
5151
go fh.detectFuseStarted(startCtx, started)
5252

5353
mCh := make(chan bool, 1)
54-
fh.wg.Add(1)
5554
go func() {
56-
defer fh.wg.Done()
5755
mCh <- fh.host.Mount(fh.mountPoint, opts)
5856
}()
5957
go func() {
@@ -66,7 +64,9 @@ func (fh *FuseHost) Start(ctx context.Context, startTimeout time.Duration) error
6664
defer fh.wg.Done()
6765
select {
6866
case <-ctx.Done():
67+
logrus.Debug("FuseHost unmounting")
6968
fh.host.Unmount()
69+
logrus.Debug("FuseHost unmount complete")
7070
case mountResult := <-mCh:
7171
if !mountResult {
7272
logrus.Errorf("fuse mount of %s failed", fh.mountPoint)

pkg/main/main.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (s *service) Mount(_ context.Context, rq *rpc.MountRequest) (*rpc.MountIden
7575
return nil, err
7676
}
7777
ctx, cancel := context.WithCancel(s.ctx)
78-
fi, err := fs.NewFTPClient(ctx, ap, rq.Directory, rq.ReadOnly, rq.ReadTimeout.AsDuration())
78+
fi, err := fs.NewFTPClient(ctx.Done(), ap, rq.Directory, rq.ReadOnly, rq.ReadTimeout.AsDuration())
7979
if err != nil {
8080
cancel()
8181
return nil, status.Error(codes.Internal, err.Error())
@@ -94,19 +94,23 @@ func (s *service) Mount(_ context.Context, rq *rpc.MountRequest) (*rpc.MountIden
9494
s.Lock()
9595
delete(s.mounts, id)
9696
s.Unlock()
97+
logrus.Debug("FuseHost Stop")
9798
host.Stop()
99+
logrus.Debug("Cancelling mount context")
98100
cancel()
99101
},
100102
}
101103
s.nextID++
102104
return &rpc.MountIdentifier{Id: id}, nil
103105
}
104106

105-
func (s *service) Unmount(_ context.Context, rq *rpc.MountIdentifier) (*emptypb.Empty, error) {
107+
func (s *service) Unmount(ctx context.Context, rq *rpc.MountIdentifier) (*emptypb.Empty, error) {
108+
logrus.Debug("Unmount called")
106109
s.Lock()
107110
m, ok := s.mounts[rq.Id]
108111
s.Unlock()
109112
if ok {
113+
logrus.Debug("cancelling mount")
110114
m.cancel()
111115
}
112116
return &emptypb.Empty{}, nil

0 commit comments

Comments
 (0)