From ca0b6d0c4a3520a87037ba72bb1f1ee10eb504dc Mon Sep 17 00:00:00 2001 From: Bitflash Date: Sat, 1 Aug 2026 21:27:29 -0300 Subject: diag: count the socket leak the way that is true, and say what it found (#100) Windows lists a wildcard-bound socket twice in MSFT_NetTCPConnection, so counting Bound rows makes the number climb whenever the node talks to more peers -- 35 of a node's 38 Bound rows shared a local port with one of its own established connections. Counting orphans instead: a 1.2.10 node at 26h held 1262 and was still taking on 30-90 an hour; nodes on 1.2.11 and 1.2.12 held 3, transient. The leak was real and 1.2.11 ended it. Adds a live-unconn column (getpeername on every tagged socket, reports 1: the listener) and rewrites the notes that said otherwise. --- src/net.cpp | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'src/net.cpp') diff --git a/src/net.cpp b/src/net.cpp index 4fe5f15..b56f9f5 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -64,30 +64,38 @@ static string SockAccountingText() long long nOpened[SOCK_SITES], nClosed[SOCK_SITES], nFailed[SOCK_SITES], nUntagged = 0; BtfSockSnapshot(nOpened, nClosed, nFailed, &nUntagged); + long long nUnconn[SOCK_SITES]; + BtfSockLiveUnconnected(nUnconn); + string str = "\n sockets by where they were created\n"; - str += " site opened closed live close failed\n"; - long long nTotalOpened = 0, nTotalClosed = 0, nTotalFailed = 0; + str += " site opened closed live close failed live unconn\n"; + long long nTotalOpened = 0, nTotalClosed = 0, nTotalFailed = 0, nTotalUnconn = 0; for (int i = 0; i < SOCK_SITES; i++) { nTotalOpened += nOpened[i]; nTotalClosed += nClosed[i]; nTotalFailed += nFailed[i]; + nTotalUnconn += nUnconn[i]; if (nOpened[i] == 0) continue; - str += strprintf(" %-24s %8lld %8lld %8lld %13lld\n", pszSockSite[i], - nOpened[i], nClosed[i], nOpened[i] - nClosed[i], nFailed[i]); + str += strprintf(" %-24s %8lld %8lld %8lld %13lld %12lld\n", pszSockSite[i], + nOpened[i], nClosed[i], nOpened[i] - nClosed[i], nFailed[i], nUnconn[i]); } - str += strprintf(" %-24s %8s %8lld %8s %13s\n", "closed but never tagged", - "-", nUntagged, "-", "-"); - str += strprintf(" %-24s %8lld %8lld %8lld %13lld\n", "TOTAL", - nTotalOpened, nTotalClosed, nTotalOpened - nTotalClosed, nTotalFailed); - - // The live total above is what this program believes it is holding. Compare - // it against what the operating system attributes to the process: on - // Windows, `Get-NetTCPConnection | ? OwningProcess -eq `. A gap there - // is the leak, and the codes below say what the failed closes hit -- 10038 - // (WSAENOTSOCK) means the handle was already gone and this close was a - // second one, which is a different bug from a descriptor that stayed. + str += strprintf(" %-24s %8s %8lld %8s %13s %12s\n", "closed but never tagged", + "-", nUntagged, "-", "-", "-"); + str += strprintf(" %-24s %8lld %8lld %8lld %13lld %12lld\n", "TOTAL", + nTotalOpened, nTotalClosed, nTotalOpened - nTotalClosed, + nTotalFailed, nTotalUnconn); + + // The live total is what this program believes it is holding. Comparing it + // against `Get-NetTCPConnection | ? OwningProcess -eq ` needs the note + // in sockcount.h first: that table lists a wildcard-bound socket twice, so + // a raw row count always looks larger than this. Count orphans, not rows. + // + // "live unconn" is a getpeername on every tagged socket. Anything above 1 + // -- the listening socket -- means this program is holding a socket it + // never connected, which is the shape the leak hunt was looking for and + // never found. if (nTotalFailed > 0) { str += " close failures by code\n"; -- cgit v1.2.3