Skip to content

Segfaults with http2 on libcurl 7.60.0 #2674

Closed

Description

@jeroen

As of recently my programs started to segfault when Debian switch to libcurl with openssl+nghttp2. I think there are may be multiple issues.

Here is a simple program (link) that does the following:

 create one multi-handle
 repeat 20x {
    - add 100 http/2 requests to the multi-handle
    - run multi handle until all requests are done
    - remove requests from the multi-handle
  }

The program works on my macos with native libcurl (curl 7.54.0 (x86_64-apple-darwin17.0) libcurl/7.54.0 LibreSSL/2.0.20 zlib/1.2.11 nghttp2/1.24.0)

However on Debian (buster) the program segfaults after a while, both when compiled with libcurl4-openssl-dev as well as libcurl4-gnutls-dev. Both versions are libcurl 7.60.0.

I can also reproduce the problem on MacOS if I build libcurl with nghttp2 support:

brew install curl --with-nghttp2
brew link --force curl

And now the program will segfault here as well:

clang test.c $(pkg-config --cflags --libs libcurl)
./a.out

# Round 0: adding 100 handles...
# Completed: 100 OK!
# Round 1: adding 100 handles...
# Completed: 100 OK!
# Round 2: adding 100 handles...
# Segmentation fault: 11

And if I then brew unlink curl and recompile, everything is fine again.

Activity

  1. jeroen commented on Jun 21, 2018

    @jeroen
    ContributorAuthor

    Here is a backtrace from the above program on macos:

    * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x228)
      * frame #0: 0x00000001000dc0f4 libcurl.4.dylib`h2_session_send + 79
        frame #1: 0x00000001000db019 libcurl.4.dylib`h2_process_pending_input + 147
        frame #2: 0x00000001000dc4f3 libcurl.4.dylib`http2_conncheck + 202
        frame #3: 0x00000001000b9211 libcurl.4.dylib`extract_if_dead + 81
        frame #4: 0x00000001000b7b62 libcurl.4.dylib`create_conn + 11961
        frame #5: 0x00000001000b4c35 libcurl.4.dylib`Curl_connect + 30
        frame #6: 0x00000001000c75de libcurl.4.dylib`multi_runsingle + 987
        frame #7: 0x00000001000c7110 libcurl.4.dylib`curl_multi_perform + 133
        frame #8: 0x0000000100000c92 a.out`main at test.c:57
        frame #9: 0x00007fff59734015 libdyld.dylib`start + 1
    

    Also sometimes it crashes here:

    (lldb) bt
    * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
      * frame #0: 0x00000001000c9397 libcurl.4.dylib`Curl_set_in_callback + 28
        frame #1: 0x00000001000ae9b4 libcurl.4.dylib`showit + 46
        frame #2: 0x00000001000adf2b libcurl.4.dylib`Curl_infof + 227
        frame #3: 0x00000001000dc4ce libcurl.4.dylib`http2_conncheck + 165
        frame #4: 0x00000001000b9211 libcurl.4.dylib`extract_if_dead + 81
        frame #5: 0x00000001000b7b62 libcurl.4.dylib`create_conn + 11961
        frame #6: 0x00000001000b4c35 libcurl.4.dylib`Curl_connect + 30
        frame #7: 0x00000001000c75de libcurl.4.dylib`multi_runsingle + 987
        frame #8: 0x00000001000c7110 libcurl.4.dylib`curl_multi_perform + 133
        frame #9: 0x0000000100000c92 a.out`main at test.c:57
        frame #10: 0x00007fff59734015 libdyld.dylib`start + 1
    
  2. bagder commented on Jun 21, 2018

    @bagder
    Member

    This commit might be the fix you want: 2c15693. It fixed basically the same symptoms.

  3. jeroen commented on Jun 21, 2018

    @jeroen
    ContributorAuthor

    I tried rebuilding against the master branch. That fixes the example but my R program still crashes. I haven't been able to translate it to a C example yet but here is the backtrace:

    (lldb) bt
    * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0xb8)
      * frame #0: 0x0000000106937ebc libcurl.4.dylib`Curl_conncache_bundle_size + 12
        frame #1: 0x0000000106917938 libcurl.4.dylib`create_conn + 13880
        frame #2: 0x000000010691428c libcurl.4.dylib`Curl_connect + 30
        frame #3: 0x00000001069264f6 libcurl.4.dylib`multi_runsingle + 987
        frame #4: 0x0000000106926028 libcurl.4.dylib`curl_multi_perform + 133
        frame #5: 0x00000001039dfdfa curl.so`R_multi_run + 1530
        frame #6: 0x0000000100123543 libR.dylib`R_doDotCall(ofun=<unavailable>, nargs=<unavailable>, cargs=<unavailable>, call=0x0000000106146d98) at dotcode.c:576 [opt]
        frame #7: 0x000000010016352b libR.dylib`bcEval(body=<unavailable>, rho=0x00000001060c0318, useCache=<unavailable>) at eval.c:7279 [opt]
        frame #8: 0x000000010015386b libR.dylib`Rf_eval(e=<unavailable>, rho=<unavailable>) at eval.c:624 [opt]
        frame #9: 0x00000001001661bc libR.dylib`R_execClosure(call=0x0000000106142200, newrho=0x00000001060c0318, sysparent=<unavailable>, rho=<unavailable>, arglist=<unavailable>, op=<unavailable>) at eval.c:0 [opt]
        frame #10: 0x0000000100158883 libR.dylib`bcEval(body=<unavailable>, rho=0x0000000101062f98, useCache=<unavailable>) at eval.c:6739 [opt]
        frame #11: 0x000000010015386b libR.dylib`Rf_eval(e=<unavailable>, rho=<unavailable>) at eval.c:624 [opt]
        frame #12: 0x00000001001645a3 libR.dylib`forcePromise(e=0x00000001060c0660) at eval.c:520 [opt]
        frame #13: 0x00000001001538f2 libR.dylib`Rf_eval(e=0x00000001060c0660, rho=<unavailable>) at eval.c:660 [opt]
        frame #14: 0x00000001001a873a libR.dylib`do_usemethod(call=0x000000010612e708, op=<unavailable>, args=<unavailable>, env=0x00000001060c0580) at objects.c:522 [opt]
        frame #15: 0x0000000100158e39 libR.dylib`bcEval(body=<unavailable>, rho=0x00000001060c0580, useCache=<unavailable>) at eval.c:6791 [opt]
        frame #16: 0x000000010015386b libR.dylib`Rf_eval(e=<unavailable>, rho=<unavailable>) at eval.c:624 [opt]
        frame #17: 0x00000001001661bc libR.dylib`R_execClosure(call=0x000000010612e7e8, newrho=0x00000001060c0580, sysparent=<unavailable>, rho=<unavailable>, arglist=<unavailable>, op=<unavailable>) at eval.c:0 [opt]
        frame #18: 0x0000000100158883 libR.dylib`bcEval(body=<unavailable>, rho=0x0000000101062f98, useCache=<unavailable>) at eval.c:6739 [opt]
        frame #19: 0x000000010016801c libR.dylib`R_compileAndExecute(call=<unavailable>, rho=<unavailable>) at eval.c:1406 [opt]
        frame #20: 0x00000001001680b9 libR.dylib`do_while(call=0x00000001059ff248, op=0x000000010102cb80, args=0x00000001059ff280, rho=0x0000000101062f98) at eval.c:2308 [opt]
        frame #21: 0x0000000100153a5e libR.dylib`Rf_eval(e=<unavailable>, rho=0x0000000101062f98) at eval.c:699 [opt]
        frame #22: 0x0000000100196efa libR.dylib`Rf_ReplIteration(rho=0x0000000101062f98, savestack=<unavailable>, browselevel=0, state=0x00007ffeefbfe7d0) at main.c:258 [opt]
        frame #23: 0x00000001001984df libR.dylib`run_Rmainloop [inlined] R_ReplConsole(rho=<unavailable>, savestack=0, browselevel=0) at main.c:308 [opt]
        frame #24: 0x0000000100198476 libR.dylib`run_Rmainloop at main.c:1082 [opt]
        frame #25: 0x0000000100000f5b R`main + 27
        frame #26: 0x00007fff59734015 libdyld.dylib`start + 1
        frame #27: 0x00007fff59734015 libdyld.dylib`start + 1
    
  4. bagder commented on Jun 21, 2018

    @bagder
    Member

    Try this additional patch, which really is a follow-up to that commit I mentioned above:

    diff --git a/lib/url.c b/lib/url.c
    index 0cab0a303..f1a6e8db4 100644
    --- a/lib/url.c
    +++ b/lib/url.c
    @@ -1463,10 +1463,11 @@ ConnectionExists(struct Curl_easy *data,
       }
     
       if(chosen) {
         /* mark it as used before releasing the lock */
         chosen->inuse = TRUE;
    +    chosen->data = data; /* own it! */
         Curl_conncache_unlock(needle);
         *usethis = chosen;
         return TRUE; /* yes, we found one to use! */
       }
       Curl_conncache_unlock(needle);
  5. jeroen commented on Jun 21, 2018

    @jeroen
    ContributorAuthor

    Yes this seems to resolve my immediate crash, so that is an improvement :-) I would need to investigate if this also resolves my other issues.

  6. bagder commented on Jun 21, 2018

    @bagder
    Member

    Great! I'll hold off merging this for a while to see if you find something else as well...

  7. added a commit that references this issue on Jun 21, 2018
    fa91204
  8. jeroen commented on Jun 21, 2018

    @jeroen
    ContributorAuthor

    Everything looks good on MacOS now. The crashes were more frequent on Debian, but I don't have time to build a version of libcurl with nghttp2 from source on Linux. Perhaps once this fix is in master I can ask the Debian maintainer of libcurl to give it a try.

  9. jeroen commented on Jun 22, 2018

    @jeroen
    ContributorAuthor

    I'm still getting an occosional crash. Might be another problem. Here is a backtrace:

    (lldb) bt
    * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
      * frame #0: 0x0000000105f9494f libcurl.4.dylib`Curl_http_readwrite_headers + 1862
        frame #1: 0x0000000105fa6f51 libcurl.4.dylib`Curl_readwrite + 931
        frame #2: 0x0000000105faec71 libcurl.4.dylib`multi_runsingle + 2902
        frame #3: 0x0000000105fae028 libcurl.4.dylib`curl_multi_perform + 133
        frame #4: 0x00000001042e02ea curl.so`R_multi_run + 1530
        frame #5: 0x0000000100123543 libR.dylib`R_doDotCall(ofun=<unavailable>, nargs=<unavailable>, cargs=<unavailable>, call=0x00000001064ec950) at dotcode.c:576 [opt]
        frame #6: 0x000000010016352b libR.dylib`bcEval(body=<unavailable>, rho=0x00000001064e3408, useCache=<unavailable>) at eval.c:7279 [opt]
        frame #7: 0x000000010015386b libR.dylib`Rf_eval(e=<unavailable>, rho=<unavailable>) at eval.c:624 [opt]
        frame #8: 0x00000001001661bc libR.dylib`R_execClosure(call=0x00000001064fd4a8, newrho=0x00000001064e3408, sysparent=<unavailable>, rho=<unavailable>, arglist=<unavailable>, op=<unavailable>) at eval.c:0 [opt]
    
  10. bagder commented on Jun 22, 2018

    @bagder
    Member

    so which pointer is NULL there? http.c:1862 doesn't seem to be the same line in my code...

  11. jeroen commented on Jun 22, 2018

    @jeroen
    ContributorAuthor

    I think that is a byte offset, not a line number. Itried building libcurl with --enable-debug --enable-curldebug --disable-hidden-symbols but I'm not getting a better back trace. Here is some output other debug output at least:

    stream: adjusting kept idle streams num_idle_streams=0, max=100
    recv: connection recv_window_size=580188, local_window=1073741824
    stream: adjusting kept idle streams num_idle_streams=0, max=100
    recv: [IB_READ_DATA]
    recv: readlen=1179, payloadleft=2841
    recv: data_readlen=1179
    stream: adjusting kept idle streams num_idle_streams=0, max=100
    recv: connection recv_window_size=581367, local_window=1073741824
    stream: adjusting kept idle streams num_idle_streams=0, max=100
    recv: [IB_READ_DATA]
    recv: readlen=1179, payloadleft=1662
    recv: data_readlen=1179
    stream: adjusting kept idle streams num_idle_streams=0, max=100
    Process 655 stopped
    * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
        frame #0: 0x000000010788da55 libcurl.4.dylib`Curl_http_readwrite_headers + 1886
    libcurl.4.dylib`Curl_http_readwrite_headers:
    ->  0x10788da55 <+1886>: movl   $0x2, (%rax)
        0x10788da5b <+1892>: movq   -0x48(%rbp), %rax
        0x10788da5f <+1896>: movl   (%rax), %eax
        0x10788da61 <+1898>: cmpl   $0x130, %eax              ; imm = 0x130
    Target 0: (R) stopped.
    (lldb) bt
    * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
      * frame #0: 0x000000010788da55 libcurl.4.dylib`Curl_http_readwrite_headers + 1886
        frame #1: 0x00000001078a0e3c libcurl.4.dylib`Curl_readwrite + 918
        frame #2: 0x00000001078a9c41 libcurl.4.dylib`multi_runsingle + 2623
        frame #3: 0x00000001078a910f libcurl.4.dylib`curl_multi_perform + 133
        frame #4: 0x00000001047d52ea curl.so`R_multi_run + 1530
    
  12. dtzWill commented on Jun 22, 2018

    @dtzWill
    Contributor

    I can reproduce (using multi-stress.cpp, Linux) using 7.60. Failures I'm seeing appear similar to the ones discussed in #2669 and recently fixed on master branch.

    When building against curl 7.60 + first fix on that PR => can't seem to reproduce
    When building against curl 'master' => can't seem to reproduce

    Testing using address-sanitizer and debug configure flags.

    If you're able to reproduce on Linux using patched curl (or latest git), please let me know --and any suggestions of things to "encourage" seeing this bug would help reproduce--which is important for ability to produce a fix.

  13. 21 remaining items

  14. ThomasObenaus commented on Jul 15, 2018

    @ThomasObenaus

    @bagder yes that's really cool to hear.
    Do you eventually have these fixes on a branch I can use for testing as well? Maybe the #2688 is then solved too.

  15. bagder commented on Jul 16, 2018

    @bagder
    Member

    They will be merged into master. I'll let you know when it's time! (I've been out and about a bit the past few days)

  16. ReneReusner commented on Jul 16, 2018

    @ReneReusner

    Hello

    I'm from the same company as ThomasObenaus, and helped debugging the crash too.

    The crash resulted in accessing a former valid, but now invalid (deleted) Curl_easy handle. After a timeout thomas did a curl_multi_remove_handle and a curl_easy_cleanup.
    Sometimes the curl_multi_remove_handle did not reset the stream_userdata to zero for this stream id.

    When curl wants to close the session, or just find a frame with this now invalid stream id in the session, albeit handling a another Curl_easy handle, it called a callback with a invalid Curl_easy handle -> BOOM,

    My fast workaround is, track the session and stream_id in Curl_easy and clear the stream_userdata it when removing the handle in curl_multi_remove_handle. Its just a bell wire around the engine block, but it seems to work for a now. But i prefer a proper bugfix too :-).

  17. bagder commented on Jul 16, 2018

    @bagder
    Member

    @ReneReusner I already landed a few fixes and there are more coming. Once I've landed these fixes I will be very interested in further debugging and testing to make sure we iron out all the last wrinkles.

  18. bagder commented on Jul 20, 2018

    @bagder
    Member

    I just landed #2751 as commit 7b9bc96 and now the crashes are if not gone, at least much less likely to occur.

  19. jeroen commented on Jul 22, 2018

    @jeroen
    ContributorAuthor

    Thanks, looking good. Are you still planning on a patch release soon?

  20. ThomasObenaus commented on Jul 25, 2018

    @ThomasObenaus

    Sadly it does not solve #2688. I got the same bt even with the fixes of 7b9bc96.

  21. jeroen commented on Jul 25, 2018

    @jeroen
    ContributorAuthor

    @ThomasObenaus Did you try 81377ca ?

  22. ThomasObenaus commented on Jul 25, 2018

    @ThomasObenaus

    @jeroen I tried with current master including 81377ca ,sadly no success :-(

  23. bagder commented on Jul 25, 2018

    @bagder
    Member

    Let me remind you that I can't reproduce your problem. With @jeroen's example I managed to find and fix a whole slew of issues, but as they've gradually been fixed that sample program now works really well. I'm not entirely convinced it runs 100% fine all the time yet, but without a way to reproduce your case @ThomasObenaus, we risk having to live with that until someone can reproduce it.

  24. ThomasObenaus commented on Jul 26, 2018

    @ThomasObenaus

    @bagder I totally understand this. But yet I found no other way to reproduce than the one I described (which is not transferable to you).

    Maybe one last question:
    In the logs I see that, when cleaning up the easy_handle, it fails on removing itself from the stream (the call to nghttp2_session_set_stream_user_data fails). And then later when another stream is closed which still contains a reference to that easy_handle it crashes.
    I have the feeling that there is sometimes a situation where the easy_handle and the h2-session does not match or get mixed.
    Could that be the case? Or can you maybe point me at a location which I should look at for this potential problem? I'm a bit lost in the curl-code since its new for me.

  25. bagder commented on Jul 28, 2018

    @bagder
    Member

    Okay while #2688 is still happening, @jeroen has confirmed his problems are not showing up anymore so I consider this particular issue fixed.

  26. ReneReusner commented on Aug 11, 2018

    @ReneReusner

    Hello.

    I managed to get some hours at work to look in this problem again. What i had found: On a curl time out in multi.c (~1400)

          timeout_ms = Curl_timeleft(data, &now,
                                     (data->mstate <= CURLM_STATE_WAITDO)?
                                     TRUE:FALSE);
    
          if(timeout_ms < 0) {
            /* Handle timed out */
    ............
    
            /* Force connection closed if the connection has indeed been used */
            if(data->mstate > CURLM_STATE_DO) {
              streamclose(data->easy_conn, "Disconnected with pending data");
              stream_error = TRUE;
            }
            result = CURLE_OPERATION_TIMEDOUT;
            (void)multi_done(&data->easy_conn, result, TRUE);
            /* Skip the statemachine and go directly to error handling section. */
            goto statemachine_end;

    The multi_done here

    static CURLcode multi_done(struct connectdata **connp,
                              CURLcode status,  /* an error if this is called
                                                   after an error was detected */
                              bool premature)

    needs to clear the user data in the associated stream. If it don't to it, then sometimes when nghttp2 iterate over all stream in a session, sometimes it will find a "stale" user_data and may call a callback with a deleted Curl_easy -> BOOM.

    I added it in

    void Curl_http2_done(struct connectdata *conn, bool premature)
    {

    Please also take a look in multi.c ~ 1350

    static CURLMcode multi_runsingle(struct Curl_multi *multi,
                                     struct timeval now,
                                     struct Curl_easy *data)
    ...
          data->state.pipe_broke = FALSE;
          data->easy_conn = NULL;
          /* My comment for a debug assertion
             clear http2 session infos, the http2 session is already gone,
             but the assert in http2.c:1979
             assert(!(conn->data->tmp1) || (conn->data->tmp1 == h2)); & co
             can still bite us, if we get a new connection to continue */

    On a broken pipe, Cause i'm not familiar with curl, i don't know if the associated connection can be still used? If yes (maybe its placed in the connection cache), we need to remove the stream_userdata here too.

    One another thing. Its not a error but can break on 64bit machines a backtrace in gdb:
    sendf.c ~ line 865:

      if(data->set.fdebug) {
        return curlDebugCallback(data, type, ptr, size,  data->set.debugdata);
        /*return (*data->set.fdebug)(data, type, ptr, size,

    For example on a stale Curl_easy, set.fdebug points to a invalid address, when the cpu jump to it, it crashes yes. BUT (cause of some brain damaged ABI designers, redzone, framepointer optional) the gdb needs a "valid" PC to get the unwind information. If its not in a known region -> no backtrace. Then you need a walk_stack macro. Callbacks for debugging on 64bit machines are not always a good idea.
    A callback which point to a invalid address (for example from a old stale Curl_easy) can result in a broken backtrace.

  27. locked as resolved and limited conversation to collaborators on Nov 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions