Skip to content

Commit

Permalink
[exporterhelper] Fix shutdown hang on unstarted exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonip committed Jun 4, 2024
1 parent bb4955e commit 8f1d25e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 6 additions & 3 deletions exporter/exporterhelper/batch_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func newBatchSender(cfg exporterbatcher.Config, set exporter.CreateSettings,
logger: set.Logger,
mergeFunc: mf,
mergeSplitFunc: msf,
shutdownCh: make(chan struct{}),
shutdownCh: nil,
shutdownCompleteCh: make(chan struct{}),
stopped: &atomic.Bool{},
resetTimerCh: make(chan struct{}),
Expand All @@ -63,6 +63,7 @@ func newBatchSender(cfg exporterbatcher.Config, set exporter.CreateSettings,
}

func (bs *batchSender) Start(_ context.Context, _ component.Host) error {
bs.shutdownCh = make(chan struct{})
timer := time.NewTimer(bs.cfg.FlushTimeout)
go func() {
for {
Expand Down Expand Up @@ -227,7 +228,9 @@ func (bs *batchSender) updateActiveBatch(ctx context.Context, req Request) {

func (bs *batchSender) Shutdown(context.Context) error {
bs.stopped.Store(true)
close(bs.shutdownCh)
<-bs.shutdownCompleteCh
if bs.shutdownCh != nil {
close(bs.shutdownCh)
<-bs.shutdownCompleteCh
}
return nil
}
9 changes: 9 additions & 0 deletions exporter/exporterhelper/batch_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,15 @@ func TestBatchSender_WithBatcherOption(t *testing.T) {
}
}

func TestBatchSender_UnstartedShutdown(t *testing.T) {
be, err := newBaseExporter(defaultSettings, defaultDataType, newNoopObsrepSender,
WithBatcher(exporterbatcher.NewDefaultConfig(), WithRequestBatchFuncs(fakeBatchMergeFunc, fakeBatchMergeSplitFunc)))
require.NoError(t, err)

err = be.Shutdown(context.Background())
require.NoError(t, err)
}

// TestBatchSender_ShutdownDeadlock tests that the exporter does not deadlock when shutting down while a batch is being
// merged.
func TestBatchSender_ShutdownDeadlock(t *testing.T) {
Expand Down

0 comments on commit 8f1d25e

Please sign in to comment.