From 21c8bb63cb85f3001ddee9237b1d7e689675d293 Mon Sep 17 00:00:00 2001 From: Bitflash Date: Sat, 1 Aug 2026 13:10:23 -0300 Subject: diag: count sockets by where they were created, and whether the close took (#86) A node was found holding 1502 sockets in Windows bound state, one ephemeral port each, 9% of the machine range in nineteen hours. Reading the code did not find it: every connect() failure path here closes its socket. So count. Every socket is tagged at creation and untagged at close, across all ten sites, with a counter for closes that fail. Already rules out three explanations: nothing is created or closed behind the counter (untagged closes are zero), no close is failing, and the leaked sockets are not in the live set -- tracked live matches what the OS reports as established, while the bound pile is something the program already closed successfully. --- src/btfrv.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/btfrv.cpp') diff --git a/src/btfrv.cpp b/src/btfrv.cpp index 7efd38b..6ac3280 100644 --- a/src/btfrv.cpp +++ b/src/btfrv.cpp @@ -11,7 +11,7 @@ #ifdef _WIN32 #include #include -#define CLOSESOCK closesocket +#define CLOSESOCK BtfCloseSocket #else #include #include @@ -22,12 +22,14 @@ typedef int SOCKET; #define INVALID_SOCKET (-1) #define SOCKET_ERROR (-1) -#define CLOSESOCK ::close +#define CLOSESOCK BtfCloseSocket #ifndef SD_BOTH #define SD_BOTH SHUT_RDWR #endif #endif +#include "sockcount.h" + #include #include #include @@ -96,7 +98,7 @@ static SOCKET ConnectTo(const char* host, unsigned short port) hints.ai_socktype = SOCK_STREAM; char portstr[16]; sprintf(portstr, "%u", (unsigned)port); if (getaddrinfo(host, portstr, &hints, &res) != 0 || !res) return INVALID_SOCKET; - SOCKET s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + SOCKET s = BtfSocketTag(socket(res->ai_family, res->ai_socktype, res->ai_protocol), SOCK_RV_DIAL); if (s == INVALID_SOCKET) { freeaddrinfo(res); return INVALID_SOCKET; } // Apply a short timeout only for the connect() and initial handshake so an @@ -201,7 +203,7 @@ bool RvRelayRun(unsigned short port) if (!RvInit()) return false; g_stop = false; - g_listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + g_listen = BtfSocketTag(socket(AF_INET, SOCK_STREAM, IPPROTO_TCP), SOCK_RV_LISTEN); if (g_listen == INVALID_SOCKET) return false; int yes = 1; setsockopt(g_listen, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes)); @@ -218,7 +220,7 @@ bool RvRelayRun(unsigned short port) { struct sockaddr_in cli; socklen_t len = sizeof(cli); - SOCKET s = accept(g_listen, (struct sockaddr*)&cli, &len); + SOCKET s = BtfSocketTag(accept(g_listen, (struct sockaddr*)&cli, &len), SOCK_RV_ACCEPT); if (s == INVALID_SOCKET) { if (g_stop) break; -- cgit v1.2.3