diff options
| author | Bitflash <btf01@proton.me> | 2026-08-01 13:10:23 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-08-01 13:10:23 -0300 |
| commit | 21c8bb63cb85f3001ddee9237b1d7e689675d293 (patch) | |
| tree | 9668f6bf39635424a6180c7a7edb6c7931ea7545 /src/btfrv.cpp | |
| parent | 7131c30c33c980f0ba1d37dfc6a3fbe8c319813e (diff) | |
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.
Diffstat (limited to 'src/btfrv.cpp')
| -rw-r--r-- | src/btfrv.cpp | 12 |
1 files changed, 7 insertions, 5 deletions
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 @@ | |||
| 11 | #ifdef _WIN32 | 11 | #ifdef _WIN32 |
| 12 | #include <winsock2.h> | 12 | #include <winsock2.h> |
| 13 | #include <ws2tcpip.h> | 13 | #include <ws2tcpip.h> |
| 14 | #define CLOSESOCK closesocket | 14 | #define CLOSESOCK BtfCloseSocket |
| 15 | #else | 15 | #else |
| 16 | #include <sys/socket.h> | 16 | #include <sys/socket.h> |
| 17 | #include <netinet/in.h> | 17 | #include <netinet/in.h> |
| @@ -22,12 +22,14 @@ | |||
| 22 | typedef int SOCKET; | 22 | typedef int SOCKET; |
| 23 | #define INVALID_SOCKET (-1) | 23 | #define INVALID_SOCKET (-1) |
| 24 | #define SOCKET_ERROR (-1) | 24 | #define SOCKET_ERROR (-1) |
| 25 | #define CLOSESOCK ::close | 25 | #define CLOSESOCK BtfCloseSocket |
| 26 | #ifndef SD_BOTH | 26 | #ifndef SD_BOTH |
| 27 | #define SD_BOTH SHUT_RDWR | 27 | #define SD_BOTH SHUT_RDWR |
| 28 | #endif | 28 | #endif |
| 29 | #endif | 29 | #endif |
| 30 | 30 | ||
| 31 | #include "sockcount.h" | ||
| 32 | |||
| 31 | #include <cstring> | 33 | #include <cstring> |
| 32 | #include <cstdio> | 34 | #include <cstdio> |
| 33 | #include <string> | 35 | #include <string> |
| @@ -96,7 +98,7 @@ static SOCKET ConnectTo(const char* host, unsigned short port) | |||
| 96 | hints.ai_socktype = SOCK_STREAM; | 98 | hints.ai_socktype = SOCK_STREAM; |
| 97 | char portstr[16]; sprintf(portstr, "%u", (unsigned)port); | 99 | char portstr[16]; sprintf(portstr, "%u", (unsigned)port); |
| 98 | if (getaddrinfo(host, portstr, &hints, &res) != 0 || !res) return INVALID_SOCKET; | 100 | if (getaddrinfo(host, portstr, &hints, &res) != 0 || !res) return INVALID_SOCKET; |
| 99 | SOCKET s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | 101 | SOCKET s = BtfSocketTag(socket(res->ai_family, res->ai_socktype, res->ai_protocol), SOCK_RV_DIAL); |
| 100 | if (s == INVALID_SOCKET) { freeaddrinfo(res); return INVALID_SOCKET; } | 102 | if (s == INVALID_SOCKET) { freeaddrinfo(res); return INVALID_SOCKET; } |
| 101 | 103 | ||
| 102 | // Apply a short timeout only for the connect() and initial handshake so an | 104 | // Apply a short timeout only for the connect() and initial handshake so an |
| @@ -201,7 +203,7 @@ bool RvRelayRun(unsigned short port) | |||
| 201 | if (!RvInit()) return false; | 203 | if (!RvInit()) return false; |
| 202 | g_stop = false; | 204 | g_stop = false; |
| 203 | 205 | ||
| 204 | g_listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | 206 | g_listen = BtfSocketTag(socket(AF_INET, SOCK_STREAM, IPPROTO_TCP), SOCK_RV_LISTEN); |
| 205 | if (g_listen == INVALID_SOCKET) return false; | 207 | if (g_listen == INVALID_SOCKET) return false; |
| 206 | int yes = 1; | 208 | int yes = 1; |
| 207 | setsockopt(g_listen, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes)); | 209 | setsockopt(g_listen, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes)); |
| @@ -218,7 +220,7 @@ bool RvRelayRun(unsigned short port) | |||
| 218 | { | 220 | { |
| 219 | struct sockaddr_in cli; | 221 | struct sockaddr_in cli; |
| 220 | socklen_t len = sizeof(cli); | 222 | socklen_t len = sizeof(cli); |
| 221 | SOCKET s = accept(g_listen, (struct sockaddr*)&cli, &len); | 223 | SOCKET s = BtfSocketTag(accept(g_listen, (struct sockaddr*)&cli, &len), SOCK_RV_ACCEPT); |
| 222 | if (s == INVALID_SOCKET) | 224 | if (s == INVALID_SOCKET) |
| 223 | { | 225 | { |
| 224 | if (g_stop) break; | 226 | if (g_stop) break; |