Skip to content

Conn_recv/send_plain refactoring, USE_RECV_BEFORE_SEND_WORKAROUND in cf-socket - #10280

Closed
icing wants to merge 1 commit into
curl:masterfrom
icing:recv-before-send
Closed

icing wants to merge 1 commit into
curl:masterfrom
icing:recv-before-send

Conversation

@icing

@icing icing commented Jan 11, 2023 •

Copy link
Copy Markdown
Contributor

Refactoring of the diverse Curl_send/recv_plain() and Curl_write/read_plain() functions, fix of stalling HTTP/2 processing when USE_RECV_BEFORE_SEND_WORKAROUND is in effect. Refs #10253.

  • Curl_write_plain/Curl_read_plain are gone, usage replace with Curl_conn_send/recv() calls.
  • Curl_send_plain/Curl_recv_plain have been internalized in `cf-socket.c?
  • The "pre-receive" buffering (feature USE_RECV_BEFORE_SEND_WORKAROUND) has also been
    internalised in cf-socket.c.
  • the HTTP/2 cfilter (nghttp2) now sets data->state.drain if, at the end of an operation, the "lower" filter chain reports pending data. This fixes the stalling of h2 processing when this feature is in effect.

@bagder
bagder requested a review from jay January 12, 2023 09:19
@bagder bagder added the HTTP/2 label Jan 12, 2023
@icing icing changed the title H2 and USE_RECV_BEFORE_SEND_WORKAROUND Conn_recv/send_plain refactroring, USE_RECV_BEFORE_SEND_WORKAROUND in cf-socket Jan 12, 2023
@icing icing changed the title Conn_recv/send_plain refactroring, USE_RECV_BEFORE_SEND_WORKAROUND in cf-socket Conn_recv/send_plain refactoring, USE_RECV_BEFORE_SEND_WORKAROUND in cf-socket Jan 12, 2023
- Curl_write_plain/Curl_read_plain have been eliminated. Last code use now uses Curl_conn_send/recv
  so that requests use conn->send/revc callbacks which defaults to cfilters use.
- Curl_recv_plain/Curl_send_plain have been internalized in cf-socket.c.
- USE_RECV_BEFORE_SEND_WORKAROUND (active on Windows) has been moved into cf-socket.c. The
  pre_recv buffer is held at the socket filter context.
  `postponed_data` structures have been removed from `connectdata`.
- the hanger in HTTP/2 request handling was a result of read buffering on all sends and the multi
   handling is not prepared for this. The following happens:
   - multi preforms on a HTTP/2 easy handle
   - h2 reads and processes data
   - this leads to a send of h2 data
   - which receives and buffers before the send
   - h2 returns
   - multi selects on the socket, but no data arrives (its in the buffer already)
   the workaround now receives data in a loop as long as there is something in
   the buffer. The real fix would be for multi to change, so that `data_pending`
   is evaluated before deciding to wait on the socket.

io_buffer, optional, in cf-socket.c, http/2 sets state.drain if lower filter have pending data.

This io_buffer is only available/used when the -DUSE_RECV_BEFORE_SEND_WORKAROUND is active,
e.g. on Windows configurations. It also maintains the original checks on protocol handler
being HTTP and conn->send/recv not being replaced.

The HTTP/2 (nghttp2) cfilter now sets data->state.drain when it finds out that the "lower"
filter chain has still pending data at the end of its IO operation. This prevents the
processing from becoming stalled.
@jay

jay commented Jan 31, 2023

Copy link
Copy Markdown
Member

Sorry that I did not have time to review this. I did test it and it seems to be working but I always see a lot more of drain in the verbose output:

* http2_recv returns AGAIN for stream 1
* Curl_readwrite: forcibly told to drain data
* http2_recv: easy 0xb66030 (stream 1) win 33554432/33554432
* Curl_readwrite: forcibly told to drain data
* http2_recv: easy 0xb66030 (stream 1) win 33554432/33554432
* Curl_readwrite: forcibly told to drain data
* http2_recv: easy 0xb66030 (stream 1) win 33554432/33554432

Which is probably this so maybe it's expected now:

the HTTP/2 cfilter (nghttp2) now sets data->state.drain if, at the end of an operation, the "lower" filter chain reports pending data. This fixes the stalling of h2 processing when this feature is in effect.

Another website that can be used to reproduce the issue before this fix is curl -v -H "Connection: close" https://httpd.apache.org/ -o NUL

Though this PR fixes the issue and is a welcome improvement I am going to propose in a separate PR disabling by default USE_RECV_BEFORE_SEND_WORKAROUND in Windows builds. It has led to bugs that are hard to track down and that there is no easy way to test for. Like, this looks fixed but I can't determine for certain if it's fixed in all scenarios which is a problem. I'm basically running commands thousands of times and getting good feedback. I think that if buffering of socket data improves we can revisit enabling it by default.

@icing

icing commented Jan 31, 2023

Copy link
Copy Markdown
Contributor Author

@jay thanks for the testing. I agree that this windows workaround is hard to verify for correctness. Building on top of this PR with improvements is most welcome.

I believe a general buffering implementation in the socket filters is a Good Thing, if we can get a grip on the "pending" and "drain" handling.

@bagder

bagder commented Jan 31, 2023

Copy link
Copy Markdown
Member

Thanks!

@bagder bagder closed this in 5651a36 Jan 31, 2023
bch pushed a commit to bch/curl that referenced this pull request Jul 19, 2023
- Curl_write_plain/Curl_read_plain have been eliminated. Last code use
  now uses Curl_conn_send/recv so that requests use conn->send/revc
  callbacks which defaults to cfilters use.
- Curl_recv_plain/Curl_send_plain have been internalized in cf-socket.c.
- USE_RECV_BEFORE_SEND_WORKAROUND (active on Windows) has been moved
  into cf-socket.c. The pre_recv buffer is held at the socket filter
  context.  `postponed_data` structures have been removed from
  `connectdata`.
- the hanger in HTTP/2 request handling was a result of read buffering
  on all sends and the multi handling is not prepared for this. The
  following happens:

   - multi preforms on a HTTP/2 easy handle
   - h2 reads and processes data
   - this leads to a send of h2 data
   - which receives and buffers before the send
   - h2 returns
   - multi selects on the socket, but no data arrives (its in the buffer already)
   the workaround now receives data in a loop as long as there is something in
   the buffer. The real fix would be for multi to change, so that `data_pending`
   is evaluated before deciding to wait on the socket.

io_buffer, optional, in cf-socket.c, http/2 sets state.drain if lower
filter have pending data.

This io_buffer is only available/used when the
-DUSE_RECV_BEFORE_SEND_WORKAROUND is active, e.g. on Windows
configurations. It also maintains the original checks on protocol
handler being HTTP and conn->send/recv not being replaced.

The HTTP/2 (nghttp2) cfilter now sets data->state.drain when it finds
out that the "lower" filter chain has still pending data at the end of
its IO operation. This prevents the processing from becoming stalled.

Closes curl#10280
vszakats added a commit that referenced this pull request Nov 15, 2024
- fix socket/sockindex confusion on writes:

  The callstack used to end with `Curl_write_plain()` accepting a socket
  till 7.87.0. This call got swapped for `Curl_conn_send()`, expecting
  a sockindex. `socket_write()` was updated accordingly. Its callers
  missed it and continued operating on sockets: `do_sec_send()`,
  `sec_write()`, passing it down the stack and `Curl_conn_send()`
  resolving it as if it were a sockindex.
  It affected FTP Kerberos authentication.

  Discovered through MSVC warnings:
  ```
  curl\lib\krb5.c(652,28): warning C4244: 'function': conversion from 'curl_socket_t' to 'int', possible loss of data
  curl\lib\krb5.c(654,28): warning C4244: 'function': conversion from 'curl_socket_t' to 'int', possible loss of data
  curl\lib\krb5.c(656,26): warning C4244: 'function': conversion from 'curl_socket_t' to 'int', possible loss of data
  curl\lib\krb5.c(657,26): warning C4244: 'function': conversion from 'curl_socket_t' to 'int', possible loss of data
  curl\lib\krb5.c(665,24): warning C4244: 'function': conversion from 'curl_socket_t' to 'int', possible loss of data
  curl\lib\krb5.c(666,24): warning C4244: 'function': conversion from 'curl_socket_t' to 'int', possible loss of data
  ```
  Ref: https://github.com/curl/curl/actions/runs/11846599621/job/33014592805#step:9:32

  Follow-up to 5651a36 #10280
  Bug: #15549 (comment)
  Fixes #15582

- fix uninitialized buffer:
  ```
  curl\lib\krb5.c(288,1): warning C4701: potentially uninitialized local variable '_gssresp' used
  ```
  Ref: https://github.com/curl/curl/actions/runs/11848626645/job/33020501026?pr=15585#step:9:31

- silence unreachable code compiler warning:
  ```
  curl\lib\krb5.c(370,1): warning C4702: unreachable code
  ```
  Ref: https://github.com/curl/curl/actions/runs/11848626645/job/33020501026?pr=15585#step:9:30

Closes #15585
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
- fix socket/sockindex confusion on writes:

  The callstack used to end with `Curl_write_plain()` accepting a socket
  till 7.87.0. This call got swapped for `Curl_conn_send()`, expecting
  a sockindex. `socket_write()` was updated accordingly. Its callers
  missed it and continued operating on sockets: `do_sec_send()`,
  `sec_write()`, passing it down the stack and `Curl_conn_send()`
  resolving it as if it were a sockindex.
  It affected FTP Kerberos authentication.

  Discovered through MSVC warnings:
  ```
  curl\lib\krb5.c(652,28): warning C4244: 'function': conversion from 'curl_socket_t' to 'int', possible loss of data
  curl\lib\krb5.c(654,28): warning C4244: 'function': conversion from 'curl_socket_t' to 'int', possible loss of data
  curl\lib\krb5.c(656,26): warning C4244: 'function': conversion from 'curl_socket_t' to 'int', possible loss of data
  curl\lib\krb5.c(657,26): warning C4244: 'function': conversion from 'curl_socket_t' to 'int', possible loss of data
  curl\lib\krb5.c(665,24): warning C4244: 'function': conversion from 'curl_socket_t' to 'int', possible loss of data
  curl\lib\krb5.c(666,24): warning C4244: 'function': conversion from 'curl_socket_t' to 'int', possible loss of data
  ```
  Ref: https://github.com/curl/curl/actions/runs/11846599621/job/33014592805#step:9:32

  Follow-up to 5651a36 curl#10280
  Bug: curl#15549 (comment)
  Fixes curl#15582

- fix uninitialized buffer:
  ```
  curl\lib\krb5.c(288,1): warning C4701: potentially uninitialized local variable '_gssresp' used
  ```
  Ref: https://github.com/curl/curl/actions/runs/11848626645/job/33020501026?pr=15585#step:9:31

- silence unreachable code compiler warning:
  ```
  curl\lib\krb5.c(370,1): warning C4702: unreachable code
  ```
  Ref: https://github.com/curl/curl/actions/runs/11848626645/job/33020501026?pr=15585#step:9:30

Closes curl#15585
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

3 participants