curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Close sockets asynchronously when using libdispatch (GCD)

From: Frederik Seiffert via curl-library <>
Date: Mon, 16 Jan 2023 16:30:48 +0100

Hi all,

I’m working on a networking API using multi handles and multi sockets, and using libdispatch (GCD) to monitor socket changes (via the "dispatch source" API [1]).

When receiving CURL_POLL_REMOVE, I call dispatch_source_cancel() [2] to stop the dispatch source. As this is done asynchronously, it is required to wait for the cancellation handler before closing the socket according to the documentation:

> The cancellation handler is submitted to the source's target queue when the source's event handler has finished, indicating that it is safe to close the source's handle (file descriptor or mach port).

However libcurl closes the socket immediately after calling the socket function, and at least on Windows this causes GCD to sometimes crash because WSAEventSelect() returns WSAENOTSOCK ("Socket operation on nonsocket") here: [3].

Does anyone have a suggestion as to how to work around this? The only thing I can think of is to use CURLOPT_CLOSESOCKETFUNCTION and wait for the cancellation handler before closing the socket. Would this be the recommended approach? I’m fairly new to this topic, so I might be missing something obvious.

I found that building libcurl with "CURL_DISABLE_SOCKETPAIR" fixes most these crashes, but this seems like a poor workaround and some crashes remain.



Received on 2023-01-16