Buy commercial curl support. We
help you work out your issues, debug your libcurl applications, use the API,
port to new platforms, add new features and more. With a team lead by the
curl founder Daniel himself.
Three fixes for a share's connection pool used from several threads
- Contemporary messages sorted: [ by date ] [ by thread ] [ by subject ] [ by author ] [ by messages with attachments ]
From: Detlev Stalling via curl-library <curl-library_at_lists.haxx.se>
Date: Tue, 1 Dec 2026 13:42:46 +0000
Hi,
we use libcurl with one share (CURL_LOCK_DATA_CONNECT) for the whole
application, so that all transfers reuse the same connections. The
transfers run in several threads: some threads drive a multi handle
each, others call curl_easy_perform(). No easy or multi handle is used
by more than one thread, and we use HTTP/1.1 only, so no connection is
multiplexed.
I know that CURLSHOPT_SHARE documents this as not supported, and I have
read #4915 and #22529. We did not see problems with 8.12.1, which may
well have been luck. After the update to 8.22.0 we ran into the crash
of #22529 ourselves. I looked into what goes wrong for this usage and
found that three small changes are enough to make it work for us. I
would like to ask if you would consider taking them.
Reproducer
A stand-alone stress test: 6 threads with a multi handle each and 2
threads with curl_easy_perform(), all on one share, against a local
server that also drops, resets and delays connections. libcurl is built
with ThreadSanitizer and debug assertions (gcc 12, Linux).
On master (c280d4bbc3) every run of 20 seconds ends in a SEGV, with
several hundred ThreadSanitizer reports, among them heap-use-after-free
in Curl_cpool_return().
The changes
1. url: check the multi handle first when matching connections
url_match_conn() reads fields of connections that are in use by a
transfer of another multi handle, before it rejects them in
url_match_multiplex_needs(). The owning thread changes these fields
without the pool lock, e.g. conn->origin in url_conn_reuse_adjust().
This is the crash of #22529. The change moves the check for the
multi handle to the top.
2. conncache: attach the transfer in Curl_cpool_add()
A new connection is added to the pool and the transfer is attached
after the pool was unlocked. In between it looks idle, and another
thread closes it as dead because it has not connected yet.
url_match_result() already attaches a reused connection under the
lock; this does the same for a new one.
3. conncache: detach the transfer in Curl_cpool_return()
multi_done() detaches the transfer and then calls
Curl_cpool_return(), which takes the lock. In between, another
thread can reuse or free the connection. This one is new since
8.23.0 (d1823ebc17); before, the detach happened in the callback,
under the lock. The change moves the detach into
Curl_cpool_return().
Together they are 24 added and 5 removed lines in lib/.
Results
With the three changes the test runs without a ThreadSanitizer report:
3 runs of 60 seconds with 12 + 3 threads, 1.27 million transfers over
HTTP and 580000 over HTTPS (OpenSSL 3.6.4). With any one of the three
left out it fails again.
What this is not
I do not claim that this makes the shared pool thread-safe in general.
The test does not cover multiplexed connections used from several
threads, curl_easy_upkeep(), connect-only transfers or
CURLMOPT_NETWORK_CHANGED, and I would not suggest to change the
documentation. The three changes close windows in which a connection is
in the pool, unlocked and without a transfer, or is read while its
owner writes to it.
Code
Branch, based on master of December 1:
https://github.com/vidstallin/curl/tree/share-pool-threads
1. https://github.com/vidstallin/curl/commit/b2f508d360
2. https://github.com/vidstallin/curl/commit/c794588f3c
3. https://github.com/vidstallin/curl/commit/43fb572d99
Reproducer (tests/share-pool-stress, see the README.md there):
https://github.com/vidstallin/curl/commit/b1cdb07498
Questions
- Would you take these as pull requests, one per change?
- What would you want as tests? The reproducer is a stand-alone program
with its own Python server. I could turn it into a libtest case that
runs for a few seconds against the test suite's server, so that the
thread-sanitizer CI job covers it.
The analysis, the changes and the reproducer were made with the help of
an AI coding assistant (Claude Code). I have reviewed them, and the
numbers above are from runs on my machine.
Thanks,
Detlev
www.visageimaging.com<http://www.visageimaging.com>
Date: Tue, 1 Dec 2026 13:42:46 +0000
Hi,
we use libcurl with one share (CURL_LOCK_DATA_CONNECT) for the whole
application, so that all transfers reuse the same connections. The
transfers run in several threads: some threads drive a multi handle
each, others call curl_easy_perform(). No easy or multi handle is used
by more than one thread, and we use HTTP/1.1 only, so no connection is
multiplexed.
I know that CURLSHOPT_SHARE documents this as not supported, and I have
read #4915 and #22529. We did not see problems with 8.12.1, which may
well have been luck. After the update to 8.22.0 we ran into the crash
of #22529 ourselves. I looked into what goes wrong for this usage and
found that three small changes are enough to make it work for us. I
would like to ask if you would consider taking them.
Reproducer
A stand-alone stress test: 6 threads with a multi handle each and 2
threads with curl_easy_perform(), all on one share, against a local
server that also drops, resets and delays connections. libcurl is built
with ThreadSanitizer and debug assertions (gcc 12, Linux).
On master (c280d4bbc3) every run of 20 seconds ends in a SEGV, with
several hundred ThreadSanitizer reports, among them heap-use-after-free
in Curl_cpool_return().
The changes
1. url: check the multi handle first when matching connections
url_match_conn() reads fields of connections that are in use by a
transfer of another multi handle, before it rejects them in
url_match_multiplex_needs(). The owning thread changes these fields
without the pool lock, e.g. conn->origin in url_conn_reuse_adjust().
This is the crash of #22529. The change moves the check for the
multi handle to the top.
2. conncache: attach the transfer in Curl_cpool_add()
A new connection is added to the pool and the transfer is attached
after the pool was unlocked. In between it looks idle, and another
thread closes it as dead because it has not connected yet.
url_match_result() already attaches a reused connection under the
lock; this does the same for a new one.
3. conncache: detach the transfer in Curl_cpool_return()
multi_done() detaches the transfer and then calls
Curl_cpool_return(), which takes the lock. In between, another
thread can reuse or free the connection. This one is new since
8.23.0 (d1823ebc17); before, the detach happened in the callback,
under the lock. The change moves the detach into
Curl_cpool_return().
Together they are 24 added and 5 removed lines in lib/.
Results
With the three changes the test runs without a ThreadSanitizer report:
3 runs of 60 seconds with 12 + 3 threads, 1.27 million transfers over
HTTP and 580000 over HTTPS (OpenSSL 3.6.4). With any one of the three
left out it fails again.
What this is not
I do not claim that this makes the shared pool thread-safe in general.
The test does not cover multiplexed connections used from several
threads, curl_easy_upkeep(), connect-only transfers or
CURLMOPT_NETWORK_CHANGED, and I would not suggest to change the
documentation. The three changes close windows in which a connection is
in the pool, unlocked and without a transfer, or is read while its
owner writes to it.
Code
Branch, based on master of December 1:
https://github.com/vidstallin/curl/tree/share-pool-threads
1. https://github.com/vidstallin/curl/commit/b2f508d360
2. https://github.com/vidstallin/curl/commit/c794588f3c
3. https://github.com/vidstallin/curl/commit/43fb572d99
Reproducer (tests/share-pool-stress, see the README.md there):
https://github.com/vidstallin/curl/commit/b1cdb07498
Questions
- Would you take these as pull requests, one per change?
- What would you want as tests? The reproducer is a stand-alone program
with its own Python server. I could turn it into a libtest case that
runs for a few seconds against the test suite's server, so that the
thread-sanitizer CI job covers it.
The analysis, the changes and the reproducer were made with the help of
an AI coding assistant (Claude Code). I have reviewed them, and the
numbers above are from runs on my machine.
Thanks,
Detlev
www.visageimaging.com<http://www.visageimaging.com>
-- Unsubscribe: https://lists.haxx.se/mailman/listinfo/curl-library Etiquette: https://curl.se/mail/etiquette.htmlReceived on 2026-12-01