Skip to content

Use a separate Curl handle per thread in PycurlClient - #1972

Merged
zacchua-stripe merged 3 commits into
masterfrom
zacchua/pycurl-thread-local-handle
Nov 11, 2026
Merged

zacchua-stripe merged 3 commits into
masterfrom
zacchua/pycurl-thread-local-handle

Conversation

@zacchua-stripe

@zacchua-stripe zacchua-stripe commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Why?

PycurlClient created a single pycurl.Curl handle in its constructor and reconfigured it for every request (reset(), setopt(), perform(), getinfo()) with no synchronization. libcurl forbids using an easy handle from more than one thread at a time (docs under Handles). When one client was shared across threads, concurrent requests could overwrite each other's URL, body, and headers. One request could then go out with another request's Authorization and Stripe-Account.

What?

  • Each thread now lazily creates its own Curl handle in the client's existing thread-local storage, mirroring how RequestsClient keeps a per-thread session (link). Connections are still reused within a thread.
  • close() closes the current thread's handle.
  • Added tests for per-thread handle isolation, a deterministic reproduction of the reported interleaving, and close().

See Also

http://go/j/RUN_DEVSDK-3704

Configuration

  • skip-changefile: This PR is not a user-facing change, so there's no changefile.

PycurlClient created one pycurl.Curl handle in its constructor and
reconfigured it for every request with no synchronization. When one
client was shared across threads, concurrent requests could overwrite
each other's URL, body, and headers, sending one request with another
request's API key and Stripe-Account.

Each thread now lazily creates its own handle in the client's existing
thread-local storage, so connections are still reused within a thread.
close() closes the current thread's handle.

Closes RUN_DEVSDK-3704

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Committed-By-Agent: claude
@zacchua-stripe
zacchua-stripe marked this pull request as ready for review November 10, 2026 22:15
@zacchua-stripe
zacchua-stripe requested a review from a team as a code owner November 10, 2026 22:15
@zacchua-stripe
zacchua-stripe requested review from mark-kogan-stripe and ramya-stripe and removed request for a team November 10, 2026 22:15
@zacchua-stripe
zacchua-stripe enabled auto-merge (squash) November 11, 2026 20:12
@zacchua-stripe
zacchua-stripe merged commit 38d853b into master Nov 11, 2026
20 checks passed
@zacchua-stripe
zacchua-stripe deleted the zacchua/pycurl-thread-local-handle branch November 11, 2026 20:15
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