{"record":{"id":"3e5eaa7cc9731a7a","repo":"dotnet/runtime","slug":"socket-failed-s-n","errorCode":null,"errorMessage":"socket() failed: %s\\n","messagePattern":"socket\\(\\) failed: (.+?)\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/mono/mono/component/debugger-agent.c","lineNumber":1176,"sourceCode":"\t\t/* Connect to the specified address */\n\t\t/* FIXME: Respect the timeout */\n\t\ttime_t startTime = time(NULL);\n\t\tuint32_t elapsedTime;\n\t\tdo {\n\t\t\tPRINT_DEBUG_MSG (1, \"Trying to connect - %d.\\n\", port);\n\t\t\tfor (rp = result->entries; rp != NULL; rp = rp->next) {\n\t\t\t\tMonoSocketAddress sockaddr;\n\t\t\t\tsocklen_t sock_len;\n\n\t\t\t\tMONO_ENTER_GC_UNSAFE;\n\t\t\t\tmono_debugger_socket_address_init (&sockaddr, &sock_len, rp->family, &rp->address, port);\n\t\t\t\tMONO_EXIT_GC_UNSAFE;\n\n\t\t\t\tsfd = socket (rp->family, rp->socktype,\n\t\t\t\t\t\t\trp->protocol);\n\t\t\t\tif (sfd == INVALID_SOCKET) {\n\t\t\t\t\tperror(\"socket\");\n\t\t\t\t\tfprintf(stderr, \"socket() failed: %s\\n\", strerror(errno));\n\t\t\t\t\tPRINT_DEBUG_MSG(1, \"socket() failed: %s\\n\", strerror(errno));\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tif (agent_config.timeout > 0)\n\t\t\t\t{\n\t\t\t\t\tstruct timeval timeout;\n\t\t\t\t\ttimeout.tv_sec  = 5;\n\t\t\t\t\ttimeout.tv_usec = 0;\n\t\t\t\t\tsetsockopt(sfd, SOL_SOCKET, SO_SNDTIMEO, (const char *)&timeout, sizeof(timeout));\n\t\t\t\t}\n\n\t\t\t\tres = connect (sfd, &sockaddr.addr, sock_len);\n\n\t\t\t\tif (res != SOCKET_ERROR)\n\t\t\t\t\tbreak;       /* Success */\n\n\t#ifdef HOST_WIN32","sourceCodeStart":1158,"sourceCodeEnd":1194,"githubUrl":"https://github.com/dotnet/runtime/blob/60108ba66eb7d1d12f595480091b4ad80a24b172/src/mono/mono/component/debugger-agent.c#L1158-L1194","documentation":"In Mono's debugger-agent, when configured in client/connect mode the agent iterates resolved addresses and calls socket(family,socktype,protocol) for each. If socket() returns INVALID_SOCKET it perror+s and fprintf+s errno, then continues to the next address (the loop does not abort on a single failure). It only becomes fatal if no address succeeds, at which point the agent exits.","triggerScenarios":"socket() fails per-address: protocol family not supported by kernel (EAFNOSUPPORT/EPROTONOSUPPORT/EINVAL), process out of file descriptors (EMFILE/ENFILE), or out of memory (ENOMEM). Common when a resolved address family (e.g. IPv6 AF_INET6) is disabled in the container.","commonSituations":"Mono debugger configured with --debugger-agent=transport=dtcp,address=host:port on a host with IPv6 disabled but DNS returns AAAA first; fd exhaustion from long-lived app with many sockets; connecting to an address family the sandbox forbids; older kernels without AF_UNIX support for the requested type.","solutions":["Force an address family the host supports: use an IPv4 literal (127.0.0.1 / host IP) in the debugger agent address, or enable IPv6 in the container.","Raise the process fd limit (ulimit -n) if the cause is EMFILE.","Prefer the socket transport (unix domain socket) for local debugging to avoid network-family issues.","Check the full error chain: a single socket() failure is retried, but if all addresses fail the agent exits with 'Unable to connect'."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// The agent already retries across resolved addresses; pre-validate connectivity\n// and family support before launching with the debugger agent.\n#include <sys/socket.h>\n#include <errno.h>\nstatic bool FamilySupported(int family) {\n    int fd = socket(family, SOCK_STREAM, 0);\n    if (fd < 0) return false;\n    close(fd);\n    return true;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer IPv4 literals (127.0.0.1) in the debugger agent address to avoid unsupported families.","Use a unix domain socket transport for local debugging to bypass network-family limits.","Raise ulimit -n if the process opens many sockets (EMFILE).","Enable IPv6 in the container if you must connect to IPv6-only hosts."],"tags":["mono","debugger-agent","socket","network","ipv6","file-descriptors"],"backgroundTag":null,"analyzedSha":"60108ba66eb7d1d12f595480091b4ad80a24b172","analyzedAt":"2026-08-10T18:54:11.478Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}