cf-socket: optimize curlx_nonblock() and check its return error#13942
cf-socket: optimize curlx_nonblock() and check its return error#13942panjf2000 wants to merge 1 commit intocurl:masterfrom
Conversation
icing
left a comment
There was a problem hiding this comment.
I agree with you that saving syscalls in a filter that is involved heavily is a good idea. But for this change to be reliable, it would really be necessary that the socket is only manipulated by the filter itself.
Unfortunately, this is not the case in libcurl for historical reasons. If you scan the code for calls of curlx_nonblock(), you'll find several locations outside cf-socket.c.
The other observation I had when making the shutdown change in cf-socket is that Windows sometimes seems to block on receives unless the setting to nonblocking just before did indeed succeed. That is the reason for the final receive only being done then.
Due to this, I think using a flag for skipping the curx_nonblock() during shutdown will introduce strange edge behaviour, making it issues hard to debug.
Thanks for sharing the particularity of |
I might be wrong, but in cf-socket.c I see the flag only set, but never used outside of the shutdown function? |
|
It's used in |
2879845 to
d2760ac
Compare
|
Hi @icing, would you take a glance at the latest code changes? |
Thanks for the ping. Looking at the diff, I like the optimization in The I think you should remove |
Done. |
sock_nonblocking for cf_socket_ctx|
I am sure @bagder will get right to it after Sweden celebrates midsummer. |
OK, thanks for the notice! Have a good one! |
|
Thanks! |
Updates:
Due to the particularity of
cf_socket_shutdown(), we've decided not to usesock_nonblockingforcf_socket_shutdown(). Meanwhile, we currently don't check if the socket created bycf_socket_openis non-blocking when connecting, which can lead to unexpected behaviors (blocking connect), therefore, we can leveragesock_nonblockingfor these cases. Check out #13942 (comment)We sometimes need to check if a socket is non-blocking to determine the subsequent actions, such as the
cf_socket_shutdown(), which sets the non-blocking flag on the socket and then drains its buffer during a shutdown. It has to use system calls on the socket anyway because there is no way to tell if the current socket is non-blocking, therefore, adding a new flag for structcf_socket_ctxto indicate if the socket is non-blocking will help us eliminate some redundant system calls.This PR mainly does two things:
sock_nonblockingforcf_socket_ctxand assign proper values to it (also reference it) accordinglycurlx_nonblock(), make it handle errors properly, and avoid needless system calls