diff --git a/lib/service/service.go b/lib/service/service.go index e0e2e620828c5..5fa220cf90b86 100644 --- a/lib/service/service.go +++ b/lib/service/service.go @@ -1100,12 +1100,9 @@ func initExternalLog(ctx context.Context, auditConfig types.ClusterAuditConfig, } func (process *TeleportProcess) getCacheDir() (string, error) { - process.Lock() - if cacheDir := process.cacheDir; cacheDir != "" { - process.Unlock() + if cacheDir := process.currentCacheDir(); cacheDir != "" { return cacheDir, nil } - process.Unlock() newCacheDir, err := os.MkdirTemp(process.Config.DataDir, "cache-") if err != nil { @@ -1119,7 +1116,7 @@ func (process *TeleportProcess) getCacheDir() (string, error) { if cacheDir := process.cacheDir; cacheDir != "" { process.Unlock() if err := os.Remove(newCacheDir); err != nil { - process.log.WithError(err).Warnf("failed to clean up cache directory %v", newCacheDir) + process.log.WithError(err).Warnf("Failed to clean up cache directory %q.", newCacheDir) } return cacheDir, nil } @@ -1129,6 +1126,12 @@ func (process *TeleportProcess) getCacheDir() (string, error) { return newCacheDir, nil } +func (process *TeleportProcess) currentCacheDir() string { + process.Lock() + defer process.Unlock() + return process.cacheDir +} + // initAuthService can be called to initialize auth server service func (process *TeleportProcess) initAuthService() error { var err error @@ -3847,9 +3850,9 @@ func (process *TeleportProcess) StartShutdown(ctx context.Context) context.Conte } } - if process.cacheDir != "" { + if cacheDir := process.currentCacheDir(); cacheDir != "" { if err := os.RemoveAll(process.cacheDir); err != nil { - process.log.Warningf("Failed deleting cache directory: %v.", err) + process.log.WithError(err).Warnf("Failed to clean up cache directory %q.", cacheDir) } } }() @@ -3875,16 +3878,19 @@ func (process *TeleportProcess) Close() error { var errors []error localAuth := process.getLocalAuth() if localAuth != nil { - errors = append(errors, process.localAuth.Close()) + errors = append(errors, localAuth.Close()) } if process.storage != nil { errors = append(errors, process.storage.Close()) } - if process.cacheDir != "" { - if err := os.RemoveAll(process.cacheDir); err != nil { - errors = append(errors, trace.ConvertSystemError(err)) + if cacheDir := process.currentCacheDir(); cacheDir != "" { + // services are still running but we've been asked to close as quickly + // as possible, so we'll only give this one attempt and not exit with an + // error in case something created something in the cacheDir + if err := os.RemoveAll(cacheDir); err != nil { + process.log.WithError(err).Warnf("Failed to clean up cache directory %q.", cacheDir) } }