Skip to content

Limit the number of active curl handles#14993

Merged
edolstra merged 2 commits intomasterfrom
curl-scalability
Jan 14, 2026
Merged

Limit the number of active curl handles#14993
edolstra merged 2 commits intomasterfrom
curl-scalability

Conversation

@edolstra
Copy link
Member

Motivation

Previously, calling queryValidPaths() with a large number (e.g. 100K) of store paths failed because Nix immediately creates a TransferItem for each .narinfo, which is then registered as a handle with curl. However curl appears to scale poorly internally: even though only a few downloads are actually started (up to the connections/streams limits), it spends a lot of CPU time dealing with the inactive handles. So the curl thread is sitting at 100% CPU, the active downloads stall and time out, and everything grind to a halt.

So now we limit the number of curl handles to http-connections * 5. With this, fetching 100K .narinfo files from localhost succeeds in ~15 seconds.

Also, we now create the Activity associated with a download later. There can be a long time between the creation of TransferItem and the start of the curl download, which can lead to misleading download durations and progress bar status. So now we create the Activity and update startTime when curl actually starts the download.

Context


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

Previously, calling queryValidPaths() with a large number (e.g. 100K)
of store paths failed because Nix immediately creates a `TransferItem`
for each .narinfo, which is then registered as a handle with
curl. However curl appears to scale poorly internally: even though
only a few downloads are actually started (up to the
connections/streams limits), it spends a lot of CPU time dealing with
the inactive handles. So the curl thread is sitting at 100% CPU, the
active downloads stall and time out, and everything grind to a halt.

So now we limit the number of curl handles to http-connections *
5. With this, fetching 100K .narinfo files from localhost succeeds in
~15 seconds.
There can be a long time between the creation of `TransferItem` and
the start of the curl download, which can lead to misleading download
durations and progress bar status. So now we create the `Activity` and
update `startTime` when curl actually starts the download.
Copy link
Contributor

@xokdvium xokdvium left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could repurpose https://curl.se/libcurl/c/CURLMOPT_MAX_CONCURRENT_STREAMS.html. That does seem like the maximum number of concurrent http 2 streams. So the maximum would be max connections times CURLMOPT_MAX_CONCURRENT_STREAMS (which is 100 by default, but we could also make it configurable).

@xokdvium
Copy link
Contributor

Hm, but there would have to be a way to limit the per-connection transfers.

@edolstra
Copy link
Member Author

edolstra commented Jan 14, 2026

Yeah, I thought about using CURLMOPT_MAX_TOTAL_CONNECTIONS * CURLMOPT_MAX_CONCURRENT_STREAMS since that's the theoretical limit of how many downloads can be in progress. But that would be 2500 in the default configuration, which already gives a noticeable slowdown.

@xokdvium
Copy link
Contributor

xokdvium commented Jan 14, 2026

Oh there's also https://curl.se/libcurl/c/CURLMOPT_MAX_HOST_CONNECTIONS.html.

It's unlimited by default and doesn't seem like we set it. Could it be an issue too?

@edolstra
Copy link
Member Author

I don't think that would help. Curl does observe the connection limit, e.g. it prints debug messages like:

curl: No connections available, total of 25 reached.

It just spends a lot of time on inactive handles. Probably it has a for loop iterating over the handles somewhere.

Copy link
Contributor

@xokdvium xokdvium left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me. Left one minor comment

@xokdvium
Copy link
Contributor

Probably it has a for loop iterating over the handles somewhere.

I see. I can try poking at code and maybe file an issue for that. Maybe it's something that's pretty easy to fix upstream.

@edolstra edolstra enabled auto-merge January 14, 2026 18:42
@xokdvium
Copy link
Contributor

Oh, it occurs to me what we do a wakeupMulti on each enqueueItem. That might be one of the culprits (but I don't see how we could avoid that either).

@edolstra edolstra added this pull request to the merge queue Jan 14, 2026
Merged via the queue into master with commit 8c20219 Jan 14, 2026
18 checks passed
@edolstra edolstra deleted the curl-scalability branch January 14, 2026 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants