dotnet/runtime · error

socket() failed: %s\n

Error message

socket() failed: %s\n

What it means

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.

Source

Thrown at src/mono/mono/component/debugger-agent.c:1176

		/* Connect to the specified address */
		/* FIXME: Respect the timeout */
		time_t startTime = time(NULL);
		uint32_t elapsedTime;
		do {
			PRINT_DEBUG_MSG (1, "Trying to connect - %d.\n", port);
			for (rp = result->entries; rp != NULL; rp = rp->next) {
				MonoSocketAddress sockaddr;
				socklen_t sock_len;

				MONO_ENTER_GC_UNSAFE;
				mono_debugger_socket_address_init (&sockaddr, &sock_len, rp->family, &rp->address, port);
				MONO_EXIT_GC_UNSAFE;

				sfd = socket (rp->family, rp->socktype,
							rp->protocol);
				if (sfd == INVALID_SOCKET) {
					perror("socket");
					fprintf(stderr, "socket() failed: %s\n", strerror(errno));
					PRINT_DEBUG_MSG(1, "socket() failed: %s\n", strerror(errno));
					continue;
				}

				if (agent_config.timeout > 0)
				{
					struct timeval timeout;
					timeout.tv_sec  = 5;
					timeout.tv_usec = 0;
					setsockopt(sfd, SOL_SOCKET, SO_SNDTIMEO, (const char *)&timeout, sizeof(timeout));
				}

				res = connect (sfd, &sockaddr.addr, sock_len);

				if (res != SOCKET_ERROR)
					break;       /* Success */

	#ifdef HOST_WIN32

View on GitHub (pinned to 60108ba66e)

Solutions

  1. 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.
  2. Raise the process fd limit (ulimit -n) if the cause is EMFILE.
  3. Prefer the socket transport (unix domain socket) for local debugging to avoid network-family issues.
  4. Check the full error chain: a single socket() failure is retried, but if all addresses fail the agent exits with 'Unable to connect'.
Defensive patterns

Strategy: retry

Validate before calling

// The agent already retries across resolved addresses; pre-validate connectivity
// and family support before launching with the debugger agent.
#include <sys/socket.h>
#include <errno.h>
static bool FamilySupported(int family) {
    int fd = socket(family, SOCK_STREAM, 0);
    if (fd < 0) return false;
    close(fd);
    return true;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/runtime@60108ba66e (2026-08-10). Data as JSON: /api/errors/3e5eaa7cc9731a7a. Report an issue: GitHub.