Skip to content

Commit 711cfb5

Browse files
Yashika0724Oleg.Chumin
authored andcommitted
[fix] Ensure Badger maintenance is stopped before existing Close() (jaegertracing#7940)
This PR adds synchronization between the Badger storage factory’s background goroutines and store shutdown. During shutdown (for example, pod termination or process restart), the maintenance or metrics goroutines may still be running while the Badger store is being closed. This change ensures the goroutines are allowed to exit cleanly after shutdown is signaled and before the store is closed. The behavior and APIs remain unchanged; the update only makes the shutdown sequence safer and more predictable. Thanks for taking a look, and please let me know if any adjustments are needed. Signed-off-by: Yashika0724 <ssyashika1311@gmail.com>
1 parent 782b69f commit 711cfb5

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

internal/storage/v1/badger/factory.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"os"
1212
"strings"
13+
"sync"
1314
"time"
1415

1516
"github.com/dgraph-io/badger/v4"
@@ -50,6 +51,7 @@ type Factory struct {
5051

5152
tmpDir string
5253
maintenanceDone chan bool
54+
bgWg sync.WaitGroup
5355

5456
// TODO initialize via reflection; convert comments to tag 'description'.
5557
metrics struct {
@@ -120,8 +122,15 @@ func (f *Factory) Initialize(metricsFactory metrics.Factory, logger *zap.Logger)
120122

121123
f.registerBadgerExpvarMetrics(metricsFactory)
122124

123-
go f.maintenance()
124-
go f.metricsCopier()
125+
f.bgWg.Add(2)
126+
go func() {
127+
defer f.bgWg.Done()
128+
f.maintenance()
129+
}()
130+
go func() {
131+
defer f.bgWg.Done()
132+
f.metricsCopier()
133+
}()
125134

126135
logger.Info("Badger storage configuration", zap.Any("configuration", opts))
127136

@@ -165,6 +174,7 @@ func (*Factory) CreateLock() (distributedlock.Lock, error) {
165174
// Close Implements io.Closer and closes the underlying storage
166175
func (f *Factory) Close() error {
167176
close(f.maintenanceDone)
177+
f.bgWg.Wait() // Wait for background goroutines to finish before closing store
168178
if f.store == nil {
169179
return nil
170180
}

0 commit comments

Comments
 (0)