From 8f3921d7746fdef9b2b30ea2052315f2b1301377 Mon Sep 17 00:00:00 2001 From: Andrei Matei Date: Wed, 10 Nov 2021 13:10:24 -0500 Subject: [PATCH] backupccl: put a goroutine under the stopper's control The backup processor was spawning a naked goroutine. We don't like that very much - see #58164. This patch puts that goroutine under the Stopper. One benefit is that the goroutine gets its own span, so it's resilient to the parent span being Finish()ed from under it (which was a bug until the prior commit). Release note: None --- pkg/ccl/backupccl/BUILD.bazel | 1 + pkg/ccl/backupccl/backup_processor.go | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/ccl/backupccl/BUILD.bazel b/pkg/ccl/backupccl/BUILD.bazel index 60c82cd2e0b2..387d97e952da 100644 --- a/pkg/ccl/backupccl/BUILD.bazel +++ b/pkg/ccl/backupccl/BUILD.bazel @@ -110,6 +110,7 @@ go_library( "//pkg/util/metric", "//pkg/util/protoutil", "//pkg/util/retry", + "//pkg/util/stop", "//pkg/util/syncutil", "//pkg/util/timeutil", "//pkg/util/tracing", diff --git a/pkg/ccl/backupccl/backup_processor.go b/pkg/ccl/backupccl/backup_processor.go index 5af7a9b55883..eb7508aef802 100644 --- a/pkg/ccl/backupccl/backup_processor.go +++ b/pkg/ccl/backupccl/backup_processor.go @@ -38,6 +38,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/util/ctxgroup" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/cockroach/pkg/util/log" + "github.com/cockroachdb/cockroach/pkg/util/stop" "github.com/cockroachdb/cockroach/pkg/util/timeutil" "github.com/cockroachdb/cockroach/pkg/util/tracing" "github.com/cockroachdb/errors" @@ -158,11 +159,18 @@ func (bp *backupDataProcessor) Start(ctx context.Context) { for range bp.progCh { } } - go func() { + if err := bp.flowCtx.Stopper().RunAsyncTaskEx(ctx, stop.TaskOpts{ + TaskName: "backup-worker", + SpanOpt: stop.ChildSpan, + }, func(ctx context.Context) { defer cancel() defer close(bp.progCh) bp.backupErr = runBackupProcessor(ctx, bp.flowCtx, &bp.spec, bp.progCh) - }() + }); err != nil { + bp.backupErr = err + cancel() + close(bp.progCh) + } } // Next is part of the RowSource interface.