Skip to content

CONNECT_ONLY connections can get closed when cache is full #4369

Closed
@loblik

Description

@loblik

There was a discussion recently about connection cache and CONNECT_ONLY connections. in this issue #3983 (comment)_ . And I think I've just hit the same thing.

I use CONNECT_ONLY to establish the connection and then put filedescriptor in own event loop to do some websocket communication. Recently I've noticed that sometimes connection is closed when it's returned to cache. I think this makes CURLINFO_ACTIVESOCKET unusable since Curl_conncache_return_conn does not consider CONNECT_ONLY connection as in use.

Here an example to reproduce the issue.

#include <curl/curl.h>
#include <unistd.h>
#include <fcntl.h>

void addNewConn(CURLM *multi)
{
    CURL *easyRaw = curl_easy_init();
    curl_easy_setopt(easyRaw, CURLOPT_URL, "https://curl.haxx.se/");
    curl_easy_setopt(easyRaw, CURLOPT_CONNECT_ONLY, 1L);
    curl_easy_setopt(easyRaw, CURLOPT_VERBOSE, 1L);

    curl_multi_add_handle(multi, easyRaw);
}

int main(void)
{

    CURLM *multi_handle = curl_multi_init();

    curl_multi_setopt(multi_handle, CURLMOPT_MAXCONNECTS, 1); 

    addNewConn(multi_handle);

    int still_running, repeats = 0, sockfd;

    for (;;)
    {   
      CURLMcode mc; 

      mc = curl_multi_perform(multi_handle, &still_running);

      if(mc == CURLM_OK ) { 

        struct CURLMsg *m; 

        int msgq = 0;
        m = curl_multi_info_read(multi_handle, &msgq);
        if(m && (m->msg == CURLMSG_DONE)) {
          CURL *e = m->easy_handle;
          if( repeats++)
            break;
          curl_easy_getinfo(m->easy_handle, CURLINFO_ACTIVESOCKET, &sockfd);
          printf("active socket socfd: %d\n", sockfd);
          addNewConn(multi_handle);
        }   

        mc = curl_multi_wait(multi_handle, NULL, 0, 1000, NULL);
      }   
    }   

    if (fcntl(sockfd, F_GETFD) < 0)
    {   
        printf("socfd: %d\n", sockfd);
        perror("fcntl");
    }   

    return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions