From 65f6d8717caa4623d5c0efb15f684a1afb572ad0 Mon Sep 17 00:00:00 2001 From: Edoardo Spadolini Date: Mon, 2 Dec 2024 20:33:22 +0100 Subject: [PATCH] Fix variable duration race in lib/inventory tests (#49657) --- lib/inventory/controller.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/inventory/controller.go b/lib/inventory/controller.go index 1100447ad982c..4cae69f1b404e 100644 --- a/lib/inventory/controller.go +++ b/lib/inventory/controller.go @@ -266,13 +266,11 @@ func (c *Controller) RegisterControlStream(stream client.UpstreamInventoryContro c.store.Insert(handle) // Increment the concurrent connection counter that we use to calculate the - // variable instance heartbeat duration. It's done here synchronously rather - // than in handleControlStream for the sake of tests. + // variable instance heartbeat duration. To make the behavior more easily + // testable, we increment it here and we decrement it before closing the + // stream in handleControlStream. c.instanceHBVariableDuration.Inc() - go func() { - defer c.instanceHBVariableDuration.Dec() - c.handleControlStream(handle) - }() + go c.handleControlStream(handle) } // GetControlStream gets a control stream for the given server ID if one exists (if multiple control streams @@ -375,6 +373,7 @@ func (c *Controller) handleControlStream(handle *upstreamHandle) { } } + c.instanceHBVariableDuration.Dec() for _, service := range handle.hello.Services { c.serviceCounter.decrement(service) }