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 <curl-library_at_lists.haxx.se>
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.

Thanks!
Frederik


[1] https://developer.apple.com/documentation/dispatch/dispatch_source?language=objc
[2] https://developer.apple.com/documentation/dispatch/1385604-dispatch_source_cancel?language=objc
[3] https://github.com/apple/swift-corelibs-libdispatch/blob/469c8ecfa0011f5da23acacf8658b6a60a899a78/src/event/event_windows.c#L214-L223

-- 
Unsubscribe: https://lists.haxx.se/listinfo/curl-library
Etiquette:   https://curl.se/mail/etiquette.html
Received on 2023-01-16