fix: break CurlHandle reference cycle leaking connections#22
Open
loks0n wants to merge 1 commit into
Open
Conversation
The header and write callbacks are non-static closures, so they bind $this. curl_setopt stores them on the CurlHandle, which the adapter already holds: adapter -> handle -> closure -> adapter. The cycle keeps the handle alive past refcount zero, so __destruct never runs until the cycle collector fires (10k roots) and every request leaks an open keep-alive connection (~1MB native TLS buffers, 2 fds). Measured in a long-running Swoole worker doing sequential requests: 150 requests = 309 open fds and 183MB private-dirty RSS; with static closures: 9 fds and flat memory. Neither callback uses $this. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Greptile SummaryPrevents retained cURL connections by making the header and response-body callbacks static, removing their implicit reference to the adapter instance. Confidence Score: 5/5The PR appears safe to merge with no actionable defects identified. The modified callbacks use only parameters and explicitly captured local variables, and the repository's supported PHP versions accept static closures. Important Files Changed
Reviews (1): Last reviewed commit: "fix: break CurlHandle reference cycle le..." | Re-trigger Greptile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Curl::send()registers the header and write callbacks as non-static closures. Closures declared in an instance method bind$this, andcurl_setoptstores them on theCurlHandle— which the adapter itself holds:This reference cycle keeps the handle alive after the last external reference to the
Clientis dropped, so__destruct(and itscurl_close) only runs when PHP's cycle collector fires — by default after 10,000 cycle roots accumulate. Until then, every request from a freshClientleaks an open keep-alive connection: 2 fds and ~1MB of native TLS buffers per request, invisible tomemory_get_usage().In long-running processes (Swoole workers) doing per-request
new Client(), this is catastrophic: we traced an hourly OOMKill of Appwrite Cloud's billing aggregation worker to exactly this — a single worker child held 400+ ESTABLISHED TLS connections and >100MB of native memory mid-job.Measurements (150 sequential requests, one process)
Fix
Mark both callbacks
static— neither uses$this, so this is behavior-preserving and simply prevents the binding that forms the cycle.🤖 Generated with Claude Code