PR #24776 opened by Niklas Haas (haasn)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24776
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24776.patch

This was previously rejected from my PR because "the CURLSH is only active
once the CURL * is added to the CURLM *" (paraphrased), however this is
not true - as we also curl_easy_setopt(CURLOPT_COOKIELIST) during
setup_curl(), called from the main thread, which references (and writes to)
the CURLSH, relying only on the lockfunc to synchronize access.

Fixes: 6c338a659551c8f395954698f8e0c8607c3e3f4c
Fixes: https://github.com/nxtedition/FFmpeg/issues/77
Signed-off-by: Niklas Haas <[email protected]>
Sponsored-by: nxtedition AB

# Summary of changes

Briefly describe what this PR does and why.

<!--
If this PR requires new FATE test samples, attach them to the PR and
list their target paths below (relative to the fate-suite root).

Attached filenames must match the sample's filename:

```fate-samples
# e.g. vorbis/new-sample.ogg
```
-->



>From ed7722910a6219c065ef57f6c3c3ff8820de0677 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Tue, 29 Sep 2026 20:46:32 +0200
Subject: [PATCH] avformat/libcurl: properly lock CURLSH

This was previously rejected from my PR because "the CURLSH is only active
once the CURL * is added to the CURLM *" (paraphrased), however this is
not true - as we also curl_easy_setopt(CURLOPT_COOKIELIST) during
setup_curl(), called from the main thread, which references (and writes to)
the CURLSH, relying only on the lockfunc to synchronize access.

Fixes: 6c338a659551c8f395954698f8e0c8607c3e3f4c
Fixes: https://github.com/nxtedition/FFmpeg/issues/77
Signed-off-by: Niklas Haas <[email protected]>
Sponsored-by: nxtedition AB
---
 libavformat/libcurl.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/libavformat/libcurl.c b/libavformat/libcurl.c
index 57e413df05..9fb5b81404 100644
--- a/libavformat/libcurl.c
+++ b/libavformat/libcurl.c
@@ -84,7 +84,7 @@ typedef struct CurlLoop {
     CURLM          *multi;
     CURLSH         *share;   /* shared cookies/HSTS */
 
-    pthread_mutex_t mutex;   /* guards the command queue, exit and cmd->done */
+    pthread_mutex_t mutex;   /* guards the command queue, exit, share and 
cmd->done */
     pthread_cond_t  cond;    /* signaled when a sync command completes */
     CurlCmd        *cmd_head, *cmd_tail;
     int             exit;
@@ -894,6 +894,20 @@ static int curl_dispatch(CurlLoop *loop, enum cmd_kind 
kind, CurlContext *c,
     return 0;
 }
 
+static void share_lock_callback(CURL *handle, curl_lock_data data,
+                                curl_lock_access access, void *userdata)
+{
+    CurlLoop *loop = userdata;
+    pthread_mutex_lock(&loop->mutex);
+}
+
+static void share_unlock_callback(CURL *handle, curl_lock_data data,
+                                  void *userdata)
+{
+    CurlLoop *loop = userdata;
+    pthread_mutex_unlock(&loop->mutex);
+}
+
 static CurlLoop *curl_loop_create(AVFormatContext *avfc)
 {
     CurlLoop *loop = av_mallocz(sizeof(*loop));
@@ -919,6 +933,8 @@ static CurlLoop *curl_loop_create(AVFormatContext *avfc)
     loop->share = curl_share_init();
     if (!loop->share)
         goto fail3;
+    curl_share_setopt(loop->share, CURLSHOPT_LOCKFUNC,   share_lock_callback);
+    curl_share_setopt(loop->share, CURLSHOPT_UNLOCKFUNC, 
share_unlock_callback);
     curl_share_setopt(loop->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
     curl_share_setopt(loop->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_HSTS);
 
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to