From c106532b79244dec1694f7f9dca6fc4df474f4a4 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 28 Jan 2026 19:42:16 -0800 Subject: [PATCH] fix: prevent MiniClusterCtx race conditions in command shutdown Capture global MiniClusterCtx into local variables before goroutine/select evaluation to prevent nil dereference/data race when context is reset to nil after nil check. Applied to filer, master, volume, and s3 commands. --- weed/command/filer.go | 6 ++++-- weed/command/master.go | 5 +++-- weed/command/s3.go | 3 ++- weed/command/volume.go | 5 +++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/weed/command/filer.go b/weed/command/filer.go index 8db4f8c85..3cc69ccf2 100644 --- a/weed/command/filer.go +++ b/weed/command/filer.go @@ -483,8 +483,9 @@ func (fo *FilerOptions) startFiler() { } httpS := newHttpServer(defaultMux, tlsConfig) if MiniClusterCtx != nil { + ctx := MiniClusterCtx go func() { - <-MiniClusterCtx.Done() + <-ctx.Done() httpS.Shutdown(context.Background()) grpcS.Stop() }() @@ -502,8 +503,9 @@ func (fo *FilerOptions) startFiler() { } httpS := newHttpServer(defaultMux, nil) if MiniClusterCtx != nil { + ctx := MiniClusterCtx go func() { - <-MiniClusterCtx.Done() + <-ctx.Done() httpS.Shutdown(context.Background()) grpcS.Stop() }() diff --git a/weed/command/master.go b/weed/command/master.go index 2ed59ce65..2a7511fbf 100644 --- a/weed/command/master.go +++ b/weed/command/master.go @@ -311,8 +311,9 @@ func startMaster(masterOption MasterOptions, masterWhiteList []string) { ms.Topo.HashicorpRaft.LeadershipTransfer() } }) - if MiniClusterCtx != nil { - <-MiniClusterCtx.Done() + ctx := MiniClusterCtx + if ctx != nil { + <-ctx.Done() ms.Shutdown() grpcS.Stop() } else { diff --git a/weed/command/s3.go b/weed/command/s3.go index db62e6e1c..625faa14e 100644 --- a/weed/command/s3.go +++ b/weed/command/s3.go @@ -408,8 +408,9 @@ func (s3opt *S3Options) startS3Server() bool { } httpS := newHttpServer(router, tlsConfig) if MiniClusterCtx != nil { + ctx := MiniClusterCtx go func() { - <-MiniClusterCtx.Done() + <-ctx.Done() httpS.Shutdown(context.Background()) grpcS.Stop() }() diff --git a/weed/command/volume.go b/weed/command/volume.go index 4e6311c9d..601849f28 100644 --- a/weed/command/volume.go +++ b/weed/command/volume.go @@ -319,10 +319,11 @@ func (v VolumeServerOptions) startVolumeServer(volumeFolders, maxVolumeCounts, v stopChan <- true }) - if MiniClusterCtx != nil { + ctx := MiniClusterCtx + if ctx != nil { select { case <-stopChan: - case <-MiniClusterCtx.Done(): + case <-ctx.Done(): shutdown(publicHttpDown, clusterHttpServer, grpcS, volumeServer) } } else {