Operation Already in Progress
Production Risk
Common in async network code; always use poll/select to manage non-blocking connect.
EALREADY (errno 114) is returned when connect() is called again on a non-blocking socket that already has a pending connect() in progress.
- 1Calling connect() again on a non-blocking socket while a previous connect() is still pending
- 2Race condition in async connection management
Second connect() on a non-blocking socket during pending connection.
// Non-blocking connect in progress connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)); // Returns -1, errno = EINPROGRESS (first call) // Calling connect() again before it completes: connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)); // Returns -1, errno = EALREADY
expected output
connect: Operation already in progress (EALREADY)
Fix
Use poll/select to wait for connection completion
WHEN In non-blocking connect patterns
// First connect() returns EINPROGRESS — wait for POLLOUT
connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));
struct pollfd pfd = { .fd = sockfd, .events = POLLOUT };
poll(&pfd, 1, 5000); // wait up to 5s
// Check SO_ERROR to see if connected
int err = 0; socklen_t len = sizeof(err);
getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &err, &len);
if (err == 0) { /* connected */ }Why this works
poll() POLLOUT fires when the non-blocking connect completes (success or failure); SO_ERROR tells the outcome.
✕ Call connect() in a loop without waiting
Repeated connect() returns EALREADY until the pending connect finishes; use poll() instead.
Linux Programmer Manual connect(2)
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev