From f382d7eb51cf7232936d46c6c1f77bdbc130e0a4 Mon Sep 17 00:00:00 2001 From: sukun Date: Tue, 10 Sep 2024 14:15:25 +0530 Subject: [PATCH] tcp: fix metrics for multiple calls to Close (#2953) --- p2p/transport/tcp/metrics.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/p2p/transport/tcp/metrics.go b/p2p/transport/tcp/metrics.go index 4434b037bf..213ee2200a 100644 --- a/p2p/transport/tcp/metrics.go +++ b/p2p/transport/tcp/metrics.go @@ -208,7 +208,9 @@ type tracingConn struct { isClient bool manet.Conn - tcpConn *tcp.Conn + tcpConn *tcp.Conn + closeOnce sync.Once + closeErr error } func newTracingConn(c manet.Conn, isClient bool) (*tracingConn, error) { @@ -236,8 +238,11 @@ func (c *tracingConn) getDirection() string { } func (c *tracingConn) Close() error { - collector.ClosedConn(c, c.getDirection()) - return c.Conn.Close() + c.closeOnce.Do(func() { + collector.ClosedConn(c, c.getDirection()) + c.closeErr = c.Conn.Close() + }) + return c.closeErr } func (c *tracingConn) getTCPInfo() (*tcpinfo.Info, error) {