From 0f79525f82f44c2c00c9ce2d9a9c32807aec6bfd Mon Sep 17 00:00:00 2001
From: Eugene Mutavchi <Ievgen_Mutavchi@comcast.com>
Date: Tue, 19 Aug 2025 20:02:48 +0000
Subject: [PATCH] http2: fix crash in on_data_read after connection has been
 destroyed

wrap the item's cancellable object, so `on_data_read` can be canceled
in soup_http2_message_data_close.

Also keep a reference to the message cancellable, as the SoupMessage
may be updated externally causing us to refer to the wrong cancellable
later.

Backport of ebf2d8ad8d2507787c286883a02874f0539c0206.
Backport of 3bf4a56c2d7e516dd8809f8a4f7a24b22c5b07ba.
---
 libsoup/http2/soup-client-message-io-http2.c | 32 +++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/libsoup/http2/soup-client-message-io-http2.c b/libsoup/http2/soup-client-message-io-http2.c
index 1e8e8a71..3e68409a 100644
--- a/libsoup/http2/soup-client-message-io-http2.c
+++ b/libsoup/http2/soup-client-message-io-http2.c
@@ -103,6 +103,9 @@ typedef struct {
         GByteArray *data_source_buffer;
         GError *data_source_error;
         gboolean data_source_eof;
+        GCancellable *data_source_cancellable;
+        GCancellable *data_source_message_cancellable;
+        gulong data_source_cancellable_id;
 
         SoupClientMessageIOHTTP2 *io; /* Unowned */
         SoupMessageIOCompletionFn completion_cb;
@@ -1184,6 +1187,14 @@ log_request_data (SoupHTTP2MessageData *data,
         soup_logger_log_request_data (data->logger, data->msg, (const char *)buffer, len);
 }
 
+static void
+on_data_source_cancelled (GCancellable *cancellable,
+                          gpointer      data)
+{
+        GCancellable *linked_cancellable = G_CANCELLABLE (data);
+        g_cancellable_cancel (linked_cancellable);
+}
+
 static ssize_t
 on_data_source_read_callback (nghttp2_session     *session,
                               int32_t              stream_id,
@@ -1315,9 +1326,18 @@ on_data_source_read_callback (nghttp2_session     *session,
                 } else {
                         h2_debug (data->io, data, "[SEND_BODY] Reading async");
                         g_byte_array_set_size (data->data_source_buffer, length);
+                        if (!data->data_source_cancellable) {
+                                data->data_source_cancellable = g_cancellable_new ();
+                                if (data->item->cancellable) {
+                                        data->data_source_message_cancellable = g_object_ref (data->item->cancellable);
+                                        data->data_source_cancellable_id =
+                                                g_cancellable_connect (data->data_source_message_cancellable, G_CALLBACK (on_data_source_cancelled),
+                                                                       g_object_ref (data->data_source_cancellable),  g_object_unref);
+                                }
+                        }
                         g_input_stream_read_async (in_stream, data->data_source_buffer->data, length,
                                                    get_data_io_priority (data),
-                                                   data->item->cancellable,
+                                                   data->data_source_cancellable,
                                                    (GAsyncReadyCallback)on_data_read, data);
                         data->io->in_callback--;
                         return NGHTTP2_ERR_DEFERRED;
@@ -1402,6 +1422,16 @@ soup_http2_message_data_close (SoupHTTP2MessageData *data)
                 g_clear_object (&data->body_istream);
         }
 
+        if (data->data_source_cancellable_id) {
+                g_cancellable_disconnect (data->data_source_message_cancellable, data->data_source_cancellable_id);
+                data->data_source_cancellable_id = 0;
+                g_clear_object (&data->data_source_message_cancellable);
+        }
+        if (data->data_source_cancellable) {
+                g_cancellable_cancel(data->data_source_cancellable);
+                g_clear_object(&data->data_source_cancellable);
+        }
+
         if (data->msg)
                 g_signal_handlers_disconnect_by_data (data->msg, data);
 
-- 
2.54.0

