From 8287d625a0c73d677b0cf75c0bf0be3d47effae8 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Fri, 6 Mar 2020 10:28:29 -0500 Subject: [PATCH] internal/lsp/debug: guard rpc stats with a Mutex This type was previously guarded implicitly by the global exporter mutex. With that gone, we've started seeing panics due to races in (*rpcs).Metric. Guard it with a mutex. Change-Id: I2a8c670ecfbaee9422e1f1d282b1fedb86b6a5e0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/222300 Run-TryBot: Robert Findley TryBot-Result: Gobot Gobot Reviewed-by: Ian Cottrell --- internal/lsp/debug/rpc.go | 4 ++++ internal/telemetry/metric.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/lsp/debug/rpc.go b/internal/lsp/debug/rpc.go index 39574c10cb..86829069bc 100644 --- a/internal/lsp/debug/rpc.go +++ b/internal/lsp/debug/rpc.go @@ -11,6 +11,7 @@ import ( "log" "net/http" "sort" + "sync" tlm "golang.org/x/tools/internal/lsp/telemetry" "golang.org/x/tools/internal/telemetry" @@ -42,6 +43,7 @@ var rpcTmpl = template.Must(template.Must(baseTemplate.Clone()).Parse(` `)) type rpcs struct { + mu sync.Mutex Inbound []*rpcStats Outbound []*rpcStats } @@ -91,6 +93,8 @@ type rpcCodeBucket struct { } func (r *rpcs) Metric(ctx context.Context, data telemetry.MetricData) { + r.mu.Lock() + defer r.mu.Unlock() for i, group := range data.Groups() { set := &r.Inbound if group.Get(tlm.RPCDirection) == tlm.Outbound { diff --git a/internal/telemetry/metric.go b/internal/telemetry/metric.go index 071dbbcd7e..c21303752c 100644 --- a/internal/telemetry/metric.go +++ b/internal/telemetry/metric.go @@ -4,7 +4,7 @@ package telemetry -// Data represents a single point in the time series of a metric. +// MetricData represents a single point in the time series of a metric. // This provides the common interface to all metrics no matter their data // format. // To get the actual values for the metric you must type assert to a concrete