Skip to content

Commit 5b8d2ac

Browse files
committed
Test with stat that doesn't hang.
Signed-off-by: Thomas Hallgren <[email protected]>
1 parent d53a256 commit 5b8d2ac

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

pkg/fs/fuse.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ func NewHost(fsh fuse.FileSystemInterface, mountPoint string) *FuseHost {
2929

3030
// Start will mount the filesystem on the mountPoint passed to NewHost.
3131
func (fh *FuseHost) Start(ctx context.Context, startTimeout time.Duration) error {
32-
ctx, cancel := context.WithCancel(ctx)
33-
fh.cancel = cancel
32+
ctx, fh.cancel = context.WithCancel(ctx)
3433

3534
opts := []string{
3635
"-o", "default_permissions",
3736
"-o", "auto_cache",
3837
"-o", "sync_read",
3938
"-o", "allow_root",
4039
}
40+
if logrus.GetLevel() >= logrus.DebugLevel {
41+
opts = append(opts, "-o", "debug")
42+
}
4143
if runtime.GOOS == "windows" {
4244
// WinFsp requires this to create files with the same
4345
// user as the one that starts the FUSE mount
@@ -47,9 +49,6 @@ func (fh *FuseHost) Start(ctx context.Context, startTimeout time.Duration) error
4749
startCtx, startCancel := context.WithTimeout(ctx, startTimeout)
4850
defer startCancel()
4951
go fh.detectFuseStarted(startCtx, started)
50-
if logrus.GetLevel() >= logrus.DebugLevel {
51-
opts = append(opts, "-o", "debug")
52-
}
5352

5453
mCh := make(chan bool, 1)
5554
fh.wg.Add(1)

pkg/fs/fuse_started_unix.go

+24-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ import (
1313
)
1414

1515
func (fh *FuseHost) detectFuseStarted(ctx context.Context, started chan error) {
16-
time.Sleep(100 * time.Millisecond)
17-
var st unix.Stat_t
18-
if err := unix.Stat(fh.mountPoint, &st); err != nil {
16+
st, err := statWithTimeout(ctx, fh.mountPoint, 10*time.Millisecond)
17+
if err != nil {
1918
select {
2019
case started <- fmt.Errorf("unable to stat mount point %q: %v", fh.mountPoint, err):
2120
default:
@@ -39,8 +38,7 @@ func (fh *FuseHost) detectFuseStarted(ctx context.Context, started chan error) {
3938
}
4039
return
4140
case <-ticker.C:
42-
var mountSt unix.Stat_t
43-
if err := unix.Stat(fh.mountPoint, &mountSt); err != nil {
41+
if mountSt, err := statWithTimeout(ctx, fh.mountPoint, 20*time.Millisecond); err != nil {
4442
// we don't consider a failure to stat an error here, just a cause for a retry.
4543
logrus.Debugf("unable to stat mount point %q: %v", fh.mountPoint, err)
4644
} else {
@@ -51,3 +49,24 @@ func (fh *FuseHost) detectFuseStarted(ctx context.Context, started chan error) {
5149
}
5250
}
5351
}
52+
53+
// statWithTimeout performs a normal unix.Stat but will not allow that it hangs for
54+
// more than the given timeout.
55+
func statWithTimeout(ctx context.Context, path string, timeout time.Duration) (*unix.Stat_t, error) {
56+
ctx, cancel := context.WithTimeout(ctx, timeout)
57+
defer cancel()
58+
errCh := make(chan error, 1)
59+
mountSt := new(unix.Stat_t)
60+
go func() {
61+
errCh <- unix.Stat(path, mountSt)
62+
}()
63+
select {
64+
case <-ctx.Done():
65+
return nil, ctx.Err()
66+
case err := <-errCh:
67+
if err != nil {
68+
return nil, err
69+
}
70+
return mountSt, nil
71+
}
72+
}

0 commit comments

Comments
 (0)