From efb93cb1783576203e22b3b1ededf0f00f42765f Mon Sep 17 00:00:00 2001
From: Andrei Matei <andrei@cockroachlabs.com>
Date: Tue, 22 Nov 2022 11:37:55 -0500
Subject: [PATCH] util/stop: finish task span before Stop() returns

Before this patch, a task's span was finished after the stopper
considered the task to be finished. This was a problem for a test who
wanted to assume that, once stopper.Stop() returns, all task spans are
finished - which is a reasonable contract to expect. This patch reorders
the span finish accordingly.

Fixes #83886

Release note: None
Epic: None
---
 pkg/util/stop/stopper.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkg/util/stop/stopper.go b/pkg/util/stop/stopper.go
index e388a56df282..d9aa6e5f6a84 100644
--- a/pkg/util/stop/stopper.go
+++ b/pkg/util/stop/stopper.go
@@ -478,8 +478,8 @@ func (s *Stopper) RunAsyncTaskEx(ctx context.Context, opt TaskOpts, f func(conte
 	// Call f on another goroutine.
 	taskStarted = true // Another goroutine now takes ownership of the alloc, if any.
 	go func() {
-		defer sp.Finish()
 		defer s.runPostlude()
+		defer sp.Finish()
 		defer s.recover(ctx)
 		if alloc != nil {
 			defer alloc.Release()