Skip to content

Hyper slowness issue #11203

Closed
Closed

Description

@gvanem

I did this

Trying to transfer 3 URLs like:

https://api.adsb.lol/api/0/route/SAS4133 
https://api.adsb.lol/api/0/route/BBD6810
https://api.adsb.lol/api/0/route/THY19Y

to get Airplane flight-information (in series or parallel), causes curl to hang for a really long time between each URL.
But only when built with -DUSE_HYPER !? Entering the URLs either from a config-file or set on the command-line doesn't matter.
A simple get-urls.bat file to reproduce:

@echo off
setlocal
set CURL_SSL_BACKEND=schannel
echo url=https://api.adsb.lol/api/0/route/SAS4133  > %TEMP%\curl_urls.txt
echo url=https://api.adsb.lol/api/0/route/BBD6810 >> %TEMP%\curl_urls.txt
echo url=https://api.adsb.lol/api/0/route/THY19Y  >> %TEMP%\curl_urls.txt
curl -s -K - < %TEMP%\curl_urls.txt | grep "_airport_codes_iata"

Running the above .bat file takes at least 1 minute to complete! With --parallel or not.

I expected the following

The transfer to complete in < 1 sec. as the curl.exe bundled with Win-10 does:

timer & c:\windows\system32\curl.exe -s https://api.adsb.lol/api/0/route/SAS4133 https://api.adsb.lol/api/0/route/BBD6810 https://api.adsb.lol/api/0/route/THY19Y | grep "_airport_codes_iata"  & timer
Timer 1 on:  9:42:26
  "_airport_codes_iata": "CGN-BLL-KEF",
  "_airport_codes_iata": "SFO-IST",
  "_airport_codes_iata": "BGO-SVG",
Timer 1 off:  9:42:26  Elapsed: 0:00:00,45

c:\windows\system32\curl -V
curl 8.0.1 (Windows) libcurl/8.0.1 Schannel WinIDN
Release-Date: 2023-03-20

Adding the --parallel option I'd assume the URLs would be issued in parallel. Doesn't look so to me.

Version info:

curl -V
curl 8.2.0-DEV (x86_64-pc-win32) libcurl/8.2.0-DEV (OpenSSL/3.2.0) Schannel 
  (rustls-ffi/0.10.0/rustls/0.21.0) zlib/1.2.12 brotli/1.0.9 zstd/1.5.2 c-ares/1.19.0 libssh2/1.10.1_DEV Hyper/1.0.0-rc.3
Release-Date: [unreleased]
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s scp sftp smb smbs smtp 
  smtps telnet tftp ws wss
Features: alt-svc AsynchDNS brotli HSTS HTTP2 HTTPS-proxy Kerberos Largefile libz MultiSSL NTLM SPNEGO SSL SSPI threadsafe 
  TLS-SRP UnixSockets zstd

I'm on Windows-10, 22H2 (OS-build 19045.2965).

Activity

  1. gvanem commented on May 26, 2023

    @gvanem
    ContributorAuthor

    After updating Rust with:

      rustup update
      rustup toolchain install nightly-x86_64-pc-windows-msvc
      rustup override set nightly-x86_64-pc-windows-msvc

    a git pull of Hyper's master and rebuilding everything, this issue persists.

  2. bagder commented on May 29, 2023

    @bagder
    Member

    Reproduces for me as well on Linux.

  3. icing commented on May 30, 2023

    @icing
    Contributor

    Added some debug info's to this. I see that in Curl_hyper_stream()which is called by curl's transfer loop:

    1. socket gives POLLIN event
    2. Curl_hyper_stream() is called
    3. hyper_waker_wake(h->read_waker) is called
    4. socket input reads the HTTP response
    5. hyper_executor_poll(h->exec); gives HYPER_TASK_EMPTY
    6. Curl_hyper_stream() returns
    7. time passes
    8. transfer loop calls this transfer again after 100ms or more
    9. no POLL event, no hyper_wake..., NO network IO
    10. hyper_executor_poll(h->exec); gives HYPER_TASK_RESPONSE

    this explains the "slowness" of handling hyper transfers.

    Experimentally, if I change step 5 to call hyper_executor_poll(h->exec); a second time, directly after the EMPTY result, it delivers HYPER_TASK_RESPONSE and the transfers are fast.

    This seem to be y hyper internal synchronization issue that make IO result not available though they have been processed internally. Only when calling the hyper poll function again, the result is returned.

  4. gvanem commented on Jun 1, 2023

    @gvanem
    ContributorAuthor

    @icing

    Only when calling the hyper poll function again, the result is returned.

    Hope someone (you?) is working on a fix. I'm clueless about such internal details.

  5. self-assigned this
    on Jun 1, 2023
  6. icing commented on Jun 1, 2023

    @icing
    Contributor

    @gvanem you are right. I wanted to document my observations to drag the hyper project in. I think this is a bug on their behalf, but if the insist everything is fine, we'll make a workaround in curl.

  7. icing commented on Jun 1, 2023

    @icing
    Contributor
  8. bagder commented on Jun 1, 2023

    @bagder
    Member

    We also have #10803 as known bug now. It is a memory-leak with hyper as a backend.

  9. added a commit that references this issue on Jun 19, 2023
  10. icing commented on Jun 19, 2023

    @icing
    Contributor

    On advisement of @seanmonstar I made some small changes that resolve the slowness for me. Pr in #11344.

    I see our http/1.1 test cases work, h2 has issues with connection reuse.

  11. bagder commented on Jun 20, 2023

    @bagder
    Member

    Does not fix it for @gvanem apparently

  12. reopened this on Jun 20, 2023
  13. 34 remaining items

  14. marlonbaeten commented on Oct 23, 2023

    @marlonbaeten

    @jsha nice work!

    I think your summary is correct.

    Together with @folkertdev I have tried several approaches in persisting both the hyper_executer and hyper_clientconn between requests. However, I was not able to persist these without triggering memory issues, and concluded that it requires more in-depth knowledge to do this without introducing any bugs. So our tests might not be the correct starting point. Just to summarize:

    • Keeping hyptransfer in the Curl_easy struct and removing the places where (parts) are cleared [1] still does not prevent it from being cleared [2] - it seems that Curl_open is called between requests which could cause this issue.
    • Making the hyper_clientconn part of the connectdata seems more appropriate and does help persisting the data, but triggers all kinds of memory issues that I was not able to debug.
    • I guess the hyper_executer should not be part of connectdata, since there might be multiple connections and, I think, there should only be one hyper_executer

    I hope to try out your fix for hyper this week.

    Good luck!

    [1] #11203 (comment)
    [2] #11203 (comment)

  15. bagder commented on Oct 23, 2023

    @bagder
    Member

    the Curl_easy struct and removing the places where (parts) are cleared [1] still does not prevent it from being cleared [2] - it seems that Curl_open is called between requests which could cause this issue.

    Curl_easy is a handle for a single transfer ( but it can be reused) while connectdata is for a specific connection. A transfer can use one or more connections to perform a single transfer.

    Curl_open() creates a new easy handle (it is the internal version of curl_easy_init()) and a new transfer can reuse a connection that was previously used by another transfer if the conditions are right - or create a new one.

    Making the hyper_clientconn part of the connectdata seems more appropriate and does help persisting the data, but triggers all kinds of memory issues that I was not able to debug.

    I think the libcurl story is easy enough but I can't tell much about hyper and how we are supposed to work with it since the API is undocumented unless you can do the rust-to-C translation in your head. And it seems very few can.

  16. jsha commented on Oct 24, 2023

    @jsha
    Contributor

    I've dug into this a little and feel like I made good progress, but I believe I'm running into an issue of conflicting lifecycles. If you want to view my branch-in-progress, very non-working, it's here: https://github.com/curl/curl/compare/master...jsha:curl:fix-hyper?expand=1

    Here's the lifecycle issue. @bagder and @seanmonstar please correct me if I'm wrong about these assumptions.

    Hyper has read and write callbacks. These get bundled into a hyper_io * object, which gets bundled into a hyper_clientconn_handshake *, which eventually gets bundled into a hyper_clientconn *. Once they're in there, they can't be changed for the lifetime of the hyper_clientconn *, which is the lifetime of the TCP / TLS connection.

    Also bundled into the hyper_clientconn * is a void *userp to be passed to the read and write callbacks. It is set at creation time and never changed for the lifetime of the connection.

    Right now Curl sets the void *userp to Curl_easy *data. That's handy for the callbacks (Curl_hyper_recv and Curl_hyper_send), because those delegate to Curl_read / Curl_write, which want a Curl_easy *. However, I think it's semantically wrong. A Curl_easy * represents a transfer, and one connection may serve many transfers during its lifetime.

    Really, I think the void *userp should be set to a struct connectdata *conn, since it has to be chosen once at the start of the connection and can never be changed.

    But that runs into a problem. The connectdata has fields recv and send, through which we should send all data. But they have a signature like this:

    curl/lib/urldata.h

    Lines 145 to 158 in 31d96af

    /* return the count of bytes sent, or -1 on error */
    typedef ssize_t (Curl_send)(struct Curl_easy *data, /* transfer */
    int sockindex, /* socketindex */
    const void *buf, /* data to write */
    size_t len, /* max amount to write */
    CURLcode *err); /* error to return */
    /* return the count of bytes read, or -1 on error */
    typedef ssize_t (Curl_recv)(struct Curl_easy *data, /* transfer */
    int sockindex, /* socketindex */
    char *buf, /* store data here */
    size_t len, /* max amount to read */
    CURLcode *err); /* error to return */

    In other words, to do a read or write, we need to pass a valid Curl_easy *. But if we make the hyper_clientconn * part of the connectdata instead of the Curl_easy, then it can never get a pointer to the current Curl_easy in order to pass it to recv / send.

    Is there any object we can set as the void *userp in a hyper_io * / hyper_clientconn * that lives as long as the connection, and allows us to find a Curl_easy * when we need to send / receive? I've thought about adding a pointer from connectdata to the currently functioning Curl_easy * but I figure that must be wrong or it probably would already have been done for convenience in other areas. :D

  17. bagder commented on Oct 24, 2023

    @bagder
    Member

    Great research @jsha

    In curl a single connection might have many concurrent transfers (when multiplexed) so a connection cannot itself identify a single transfer - each connection has a list transfers. Also, every ongoing transfer has a single current connection it uses (at a time). An individual connection is used by none, one or many transfers.

    I've thought about adding a pointer from connectdata to the currently functioning Curl_easy * but I figure that must be wrong or it probably would already have been done for convenience in other areas. :D

    Right. Because the connection can be used by up to several hundred concurrent transfers...

  18. icing commented on Oct 24, 2023

    @icing
    Contributor

    There are other places in curl with the same use pattern/problem. Callbacks in OpenSSL, nghttp2 and quiche.

    The solution there, all of those being connection filters, is to either:

    • set the "current" easy handle in the filter's context and look it up from there in a callback
    • have an id for the transfer (e.g. HTTP stream id) and look up the matching easy handle at the multi.

    Moving hyper from a http "protocol handler" replacement to a connection filter is not a small task. But it might be the better design.

  19. jsha commented on Oct 24, 2023

    @jsha
    Contributor

    Great, thanks for the confirmation @bagder. And thanks for pointing a way forward @icing.

    I think for the short term, we should disable HTTP/2 support in the Hyper integration, because it's broken (sends the connection preface on every request) and requires a rearchitecture to fix, which might take some time. The other alternative would be to have the HTTP/2 support send only a single request per connection. But it seems better to fall back to HTTP/1.1 where connections can be reused.

  20. jsha commented on Oct 24, 2023

    @jsha
    Contributor

    I've posted #12191 with the quick / temporary fix of disabling HTTP2 with Hyper.

  21. jsha commented on Nov 19, 2023

    @jsha
    Contributor

    I've posted hyperium/hyper#3424 adding documentation for Hyper's FFI API.

    @bagder @icing would one of you be willing to add more detail to the connection filter docs and/or cfilters.h? I looked at docs/CONNECTION-FILTERS.md and I don't understand how the connection filter API handles multiplexing of multiple requests on a single connection. Also, the docs suggest that HTTP/2 might be a connection filter, which seems to be presently the case, so maybe it needs some updating? Thanks!

  22. gvanem commented on Nov 24, 2023

    @gvanem
    ContributorAuthor

    @jsha After getting the latest Hyper + curl from git master and rebuilding everything, the speed has improved
    very much.

    The API at ADSB-LOL has changed into a new JSON-format. So with this .bat-file:

    @echo off
    setlocal
    set CURL_SSL_BACKEND=schannel
    set URLS= https://vrs-standing-data.adsb.lol/routes/SA/SAS4133.json ^
              https://vrs-standing-data.adsb.lol/routes/BB/BBD6810.json ^
              https://vrs-standing-data.adsb.lol/routes/TH/THY19Y.json
    
    echo Using 'c:\Windows\system32\curl.exe':
    timer
    c:\Windows\system32\curl.exe -s %URLS% > NUL
    timer
    
    echo.
    echo Using 'curl -- tip of git master':
    timer 
    %~dp0\src\curl -s %URLS% > NUL
    timer

    The timings are:

    Using 'c:\Windows\system32\curl.exe':
    Timer 1 off: 10:05:45  Elapsed: 0:00:00,233
    
    Using 'curl -- tip of git master':
    Timer 1 off: 10:05:46  Elapsed: 0:00:01,137
    

    Much better; not >1 minute as before.
    But the Win-10 bundled curl (w/o Hyper) is still ~5 times faster.

  23. jsha commented on Nov 27, 2023

    @jsha
    Contributor

    @gvanem thanks for verifying that things got faster.

    I'm not very surprised that the Hyper backend is still slightly slower than the nghttp2 (default) backend, because the hyper backend is now using HTTP/1.1. I'm guessing that curl serializes the requests when using HTTP/1.1 and parallelizes them when using HTTP/2.

    I think the solution will be as @icing said: to rewrite the Hyper backend as a connection filter so we can restore HTTP/2 support with the Hyper backend and have it work properly.

  24. gvanem commented on Dec 2, 2023

    @gvanem
    ContributorAuthor

    to rewrite the Hyper backend as a connection filter so we can restore HTTP/2 support with the Hyper backend and
    have it work properly.

    Has this been done? Just rebuilt everything and now it's only 20% slower than the Win-10 bundled curl:

    Using 'c:\Windows\system32\curl.exe':
    Timer 1 on: 10:18:49
    Timer 1 off: 10:18:49  Elapsed: 0:00:00,803
    
    Using 'curl -- tip of git master':
    Timer 1 on: 10:18:49
    Timer 1 off: 10:18:50  Elapsed: 0:00:00,974
    
  25. bagder commented on Feb 10, 2024

    @bagder
    Member

    This is mentioned in KNOWN_BUGS.

  26. added a commit that references this issue on Mar 25, 2024
    d54d5bf
  27. added a commit that references this issue on Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions