From 2ffc50998c30f285f45c5c5d81975bf857885aa8 Mon Sep 17 00:00:00 2001 From: Bitflash Date: Sat, 1 Aug 2026 18:43:25 -0300 Subject: diag: say why a close failed, and total what the program thinks it holds (#98) The 1.2.11 accounting answered 'which site' and stopped. On a node with 3h51m of uptime it reported 58 live sockets while Windows attributed 112 to the process, 47 of them bound and never connected, against 55 closes that had returned an error -- with nothing recording what those errors were. Close failures are now counted per code per site, and the table totals, so the live figure can be compared directly against what the OS attributes to the process. Instrumentation only. --- src/net.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/net.cpp') diff --git a/src/net.cpp b/src/net.cpp index 7a13257..4fe5f15 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -66,8 +66,12 @@ static string SockAccountingText() 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; for (int i = 0; i < SOCK_SITES; i++) { + nTotalOpened += nOpened[i]; + nTotalClosed += nClosed[i]; + nTotalFailed += nFailed[i]; if (nOpened[i] == 0) continue; str += strprintf(" %-24s %8lld %8lld %8lld %13lld\n", pszSockSite[i], @@ -75,6 +79,31 @@ static string SockAccountingText() } 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. + if (nTotalFailed > 0) + { + str += " close failures by code\n"; + for (int i = 0; i < SOCK_SITES; i++) + { + if (nFailed[i] == 0) + continue; + map mapErr; + BtfSockCloseErrors(i, mapErr); + string strCodes; + for (map::const_iterator mi = mapErr.begin(); mi != mapErr.end(); ++mi) + strCodes += strprintf("%s%d x%lld", strCodes.empty() ? "" : ", ", + mi->first, mi->second); + str += strprintf(" %-24s %s\n", pszSockSite[i], strCodes.c_str()); + } + } return str; } -- cgit v1.2.3