Re: Close sockets asynchronously when using libdispatch (GCD)

2023-01-31 Thread Dan Fandrich via curl-library
On Tue, Jan 31, 2023 at 09:58:13AM +0100, Frederik Seiffert wrote:
> Could you please explain what you mean by "compiling with a different 
> resolver"? I didn’t see any build options like that. Do you maybe mean 
> building with "CURL_DISABLE_SOCKETPAIR"?

I mean using the --disable-threaded-resolver or --enable-ares configure options
(or whatever the cmake equivalent is) to use a different DNS resolver. I
thought that it was only the default threaded resolver that used a socketpair,
but in looking through the code it seems that curL_multi_poll() also uses it so
that function would need to be avoided to avoid its use.
-- 
Unsubscribe: https://lists.haxx.se/listinfo/curl-library
Etiquette:   https://curl.se/mail/etiquette.html


Re: Close sockets asynchronously when using libdispatch (GCD)

2023-01-31 Thread Frederik Seiffert via curl-library
Hi Dan,

> Am 20.01.2023 um 18:53 schrieb Dan Fandrich via curl-library 
> :
> 
>> Unfortunately it seems that in some cases when using multi handles this 
>> "close
>> socket function" is not being used, so I don’t think this approach would 
>> fully
>> work atm:
>> https://github.com/curl/curl/blob/f8da4f2f2d0451dc0a126ae3e5077b4527ccdc86/lib/asyn-thread.c#L409-L410
> 
> That's a special case, but an interesting one. I'm not sure if that use
> overlooked the socket callback functions, or if it was added with the idea
> being that since the socket isn't used for data transfer, it doesn't need to
> use the callbacks. In any case, you can easily stop use of that socket by
> compiling with a different resolver.

Could you please explain what you mean by "compiling with a different 
resolver"? I didn’t see any build options like that. Do you maybe mean building 
with "CURL_DISABLE_SOCKETPAIR"?

Thanks!
Frederik

-- 
Unsubscribe: https://lists.haxx.se/listinfo/curl-library
Etiquette:   https://curl.se/mail/etiquette.html


Re: Close sockets asynchronously when using libdispatch (GCD)

2023-01-20 Thread Frederik Seiffert via curl-library

> Am 16.01.2023 um 19:24 schrieb Dan Fandrich via curl-library 
> :
> 
> IMHO, that sounds like a good approach. curl assumes POSIX semantics on 
> sockets
> which allow them to be closed at any time. If your environment doesn't allow
> that, then hooking in to CURLOPT_CLOSESOCKETFUNCTION sounds like a good way
> to maintain those semantics.


Unfortunately it seems that in some cases when using multi handles this "close 
socket function" is not being used, so I don’t think this approach would fully 
work atm:
https://github.com/curl/curl/blob/f8da4f2f2d0451dc0a126ae3e5077b4527ccdc86/lib/asyn-thread.c#L409-L410

Would the multi handle API also need open/close functions to be added in order 
for this to be supported? Sorry I’m not familiar with how sockets are being 
used between easy and multi handles.

Thanks,
Frederik

-- 
Unsubscribe: https://lists.haxx.se/listinfo/curl-library
Etiquette:   https://curl.se/mail/etiquette.html


Re: Close sockets asynchronously when using libdispatch (GCD)

2023-01-16 Thread Dan Fandrich via curl-library
On Mon, Jan 16, 2023 at 04:30:48PM +0100, Frederik Seiffert via curl-library 
wrote:
> 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.

IMHO, that sounds like a good approach. curl assumes POSIX semantics on sockets
which allow them to be closed at any time. If your environment doesn't allow
that, then hooking in to CURLOPT_CLOSESOCKETFUNCTION sounds like a good way
to maintain those semantics.

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

I agree. That option just stops one source of socket closes, but others will
remain.

Dan
-- 
Unsubscribe: https://lists.haxx.se/listinfo/curl-library
Etiquette:   https://curl.se/mail/etiquette.html


Close sockets asynchronously when using libdispatch (GCD)

2023-01-16 Thread Frederik Seiffert via curl-library
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