more elegant handling of mount timeouts

This commit is contained in:
Gareth
2025-08-08 00:47:21 -07:00
parent db98a5108e
commit 5aadcb5a48
2 changed files with 9 additions and 10 deletions
+2 -9
View File
@@ -114,21 +114,14 @@ func (r *RepoOrchestrator) Mount(ctx context.Context) (string, func(), error) {
return "", func() {}, r.mountErr
}
// Give mount 60 seconds to succeed, but if it succeeds allow it to run indefinitely in the background.
mountCtx, cancel := context.WithCancel(context.Background())
cancelMounting := time.AfterFunc(60*time.Second, func() {
cancel()
})
if err := r.repo.Mount(mountCtx, r.mountDir); err != nil {
mountCtx, cancel := context.WithCancel(ctx)
if err := r.repo.Mount(mountCtx, r.mountDir, 60*time.Second); err != nil {
r.logger(ctx).Warn("failed to mount repo", zap.Error(err))
r.mountErr = fmt.Errorf("mount repo %v: %w", r.repoConfig.Id, err)
cancel()
cancelMounting.Stop()
os.Remove(r.mountDir)
return "", func() {}, r.mountErr
}
cancelMounting.Stop()
r.mountTimer = time.AfterFunc(1000*time.Hour, func() {
r.mountMu.Lock()
defer r.mountMu.Unlock()
+7 -1
View File
@@ -468,7 +468,7 @@ func (r *Repo) Stats(ctx context.Context, opts ...GenericOption) (*RepoStats, er
return &stats, nil
}
func (r *Repo) Mount(ctx context.Context, dir string, opts ...GenericOption) error {
func (r *Repo) Mount(ctx context.Context, dir string, mountTimeout time.Duration, opts ...GenericOption) error {
// Check if already mounted
if info, err := os.Stat(dir); err == nil {
if info.IsDir() {
@@ -489,11 +489,17 @@ func (r *Repo) Mount(ctx context.Context, dir string, opts ...GenericOption) err
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
timeout := time.NewTimer(mountTimeout)
defer timeout.Stop()
for {
select {
case <-ctx.Done():
cmd.Process.Kill()
return ctx.Err()
case <-timeout.C:
cmd.Process.Kill()
return fmt.Errorf("mount timeout")
case <-ticker.C:
if _, err := os.Stat(dir); err == nil {
if ents, err := os.ReadDir(filepath.Join(dir, "ids")); err == nil && len(ents) > 0 {