Skip to content

CURLMOPT_PUSHFUNCTION: pushed handle no longer inherits the parent's callbacks since 8.22.0 #23183

Closed

Description

@JuanCarlosgg

I did this

Applications that accept HTTP/2 server pushes with CURLMOPT_PUSHFUNCTION crash with 8.22.0 as soon as a server actually pushes something. We hit it with PHP: SIGSEGV on musl, and "glibc detected an invalid stdio handle" on glibc. Without a push callback, or with pushes denied, everything works.

I tracked it down to fad90b666d ("h2 push: use squeaky clean easy handle", #22715): h2_duphandle() now uses curl_easy_init() instead of curl_easy_duphandle(), so the pushed handle no longer gets the parent's CURLOPT_WRITEFUNCTION / CURLOPT_HEADERFUNCTION.

The CURLMOPT_PUSHFUNCTION man page still says the new handle "has been duplicated from the parent, meaning that it has gotten all its options inherited", and PHP's ext/curl relies on exactly that. In its push callback it only changes CURLOPT_WRITEDATA / CURLOPT_WRITEHEADER to point at its own handle object and leaves the callbacks alone. With a clean handle, libcurl's default fwrite() gets called with that object as the FILE *.

C and PHP Reproducers

mkdir -p /tmp/h2 && echo main > /tmp/h2/index.html && echo pushed > /tmp/h2/pushed.txt
nghttpd --no-tls -d /tmp/h2 -p/index.html=/pushed.txt 8080 &

C reproducer (gcc -o push push.c -lcurl && ./push). The parent sets its own write and header callbacks. As the docs allow, the push callback only swaps the data pointers:

#include <stdio.h>
#include <curl/curl.h>

struct sink { size_t bytes, headers; };
static struct sink main_sink, push_sink;

static size_t write_cb(char *p, size_t s, size_t n, void *u)
{ (void)p; ((struct sink *)u)->bytes += s * n; return s * n; }

static size_t header_cb(char *p, size_t s, size_t n, void *u)
{ (void)p; ((struct sink *)u)->headers++; return s * n; }

static int push_cb(CURL *parent, CURL *easy, size_t num,
                   struct curl_pushheaders *h, void *u)
{
  (void)parent; (void)num; (void)h; (void)u;
  curl_easy_setopt(easy, CURLOPT_WRITEDATA, &push_sink);
  curl_easy_setopt(easy, CURLOPT_HEADERDATA, &push_sink);
  return CURL_PUSH_OK;
}

int main(void)
{
  CURLM *m = curl_multi_init();
  CURL *e = curl_easy_init();
  CURLMsg *msg;
  int running = 1, left;

  curl_multi_setopt(m, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
  curl_multi_setopt(m, CURLMOPT_PUSHFUNCTION, push_cb);
  curl_easy_setopt(e, CURLOPT_URL, "http://127.0.0.1:8080/index.html");
  curl_easy_setopt(e, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE);
  curl_easy_setopt(e, CURLOPT_WRITEFUNCTION, write_cb);
  curl_easy_setopt(e, CURLOPT_WRITEDATA, &main_sink);
  curl_easy_setopt(e, CURLOPT_HEADERFUNCTION, header_cb);
  curl_easy_setopt(e, CURLOPT_HEADERDATA, &main_sink);
  curl_multi_add_handle(m, e);

  while(running) {
    curl_multi_perform(m, &running);
    while((msg = curl_multi_info_read(m, &left)))
      if(msg->msg == CURLMSG_DONE && msg->easy_handle != e) {
        curl_multi_remove_handle(m, msg->easy_handle);
        curl_easy_cleanup(msg->easy_handle);
      }
    if(running)
      curl_multi_poll(m, NULL, 0, 1000, NULL);
  }
  printf("pushed: %zu headers, %zu bytes\n", push_sink.headers, push_sink.bytes);
  return 0;
}

PHP reproducer (php push.php), plain ext/curl and nothing else required:

<?php
$mh = curl_multi_init();
curl_multi_setopt($mh, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
curl_multi_setopt($mh, CURLMOPT_PUSHFUNCTION, function ($parent, $pushed, array $headers) {
    echo "push: ", implode(' ', $headers), "\n";
    return CURL_PUSH_OK;
});

$ch = curl_init('http://127.0.0.1:8080/index.html');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $ch);

do {
    curl_multi_exec($mh, $running);
    while ($info = curl_multi_info_read($mh)) {
        if ($info['handle'] !== $ch) {
            echo "pushed: ", trim(curl_multi_getcontent($info['handle'])), "\n";
            curl_multi_remove_handle($mh, $info['handle']);
        }
    }
    if ($running) {
        curl_multi_select($mh);
    }
} while ($running);

echo "main: ", trim(curl_multi_getcontent($ch)), "\n";

Results:

libcurl 8.14.1 / 8.21.0: C & PHP ok
libcurl 8.22.0 / 8.23.0-rc2: C segfault, PHP segfault right after the push: line

#0  0x0000000000000000 in ?? ()
#1  fwrite (..., f=0x5fa9c2834030 <push_sink>) at src/stdio/fwrite.c:33
#2  cw_out_cb_write (..., wcb=<fwrite>, wcb_data=<push_sink>, otype=CW_OUT_HDS, buf="HTTP/2 200 \r\n", ...) at cw-out.c:190
...
#9  h2_xfer_write_resp_hd (...) at http2.c:902
#11 on_header (..., name=":status", ...) at http2.c:1534

Changing that one line back to curl_easy_duphandle(data) fixes it.

In the real world this shows up as PHP + Symfony HttpClient crashing intermittently against sites that push (Symfony accepts pushes by default). Someone else seems to have hit it too: larspohlmann/simple-feed-reader#1146

I expected the following

The pushed handle to inherit the parent's options, as documented, or at least not crash. I get that push is going away (#22490), but until then this breaks existing users in a minor release. Could this be reverted before 8.23.0 ships? If the clean handle is meant to stay, the docs need to change, and users like PHP would need a heads-up to set their callbacks in the push callback.

curl/libcurl version

libcurl 8.22.0 and 8.23.0-rc2 (8.21.0 and 8.14.1 are fine)

operating system

Alpine Linux 3.23 / 3.24 / edge (musl). Also seen on glibc (Ubuntu 22.04) with our own libcurl 8.22.0 build.

Activity

  1. icing commented on Sep 30, 2026

    @icing
    Contributor

    Thanks for the report. I am sorry that this change cause trouble. I was not well thought out it seems. We had been stumbling into various problems with the duphandle() approach. But this cut was a little too clean.

    I made #23187 to address the shortcomings, copying over the callbacks and data that an application relies on for a pushed transfer. Maybe more need to be added. Feel free to make suggestions.

  2. self-assigned this
    on Sep 30, 2026
  3. jay commented on Sep 30, 2026

    @jay
    Member

    @JuanCarlosgg please be aware HTTP/2 push is on our deprecate list and scheduled to be removed from libcurl in March 2027.

  4. JuanCarlosgg commented on Oct 1, 2026

    @JuanCarlosgg
    Author

    Thanks, understood about the deprecation. That's fine for me; once push is gone the server just can't push, nothing breaks and users still ussing it have to update their code. The problem is only the window until then.

    I tested #23187 on top of 8.22.0 and it fixes both reproducers and the Symfony case.

    One small difference from 8.21 that remains, FYI: progress callbacks set on the parent don't fire for pushed transfers anymore. PHP sets only CURLOPT_PROGRESSDATA on the pushed handle and relies on the rest being inherited too. It doesn't crash, it's just silent, so I don't think it needs to block anything. In any case I'm fine with either this fix or going back to duphandle() as Jay mentioned in th PR.

  5. icing commented on Oct 1, 2026

    @icing
    Contributor

    I can add copying of the progress callbacks. Good suggestion.

    Update: now part of #23187

  6. added a commit that references this issue on Oct 1, 2026
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