Skip to content

fix: stats usage memory leak#10683

Merged
loks0n merged 1 commit into1.8.xfrom
fix-stats-usage-memory-leak
Oct 22, 2025
Merged

fix: stats usage memory leak#10683
loks0n merged 1 commit into1.8.xfrom
fix-stats-usage-memory-leak

Conversation

@loks0n
Copy link
Copy Markdown
Member

@loks0n loks0n commented Oct 22, 2025

No description provided.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Oct 22, 2025

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Removed per-project state cleanup from inside the usort comparator in commitToDb (file: src/Appwrite/Platform/Workers/StatsUsage.php). Cleanup is now performed exclusively in a finally block after processing each project batch, guaranteeing the per-project entry is unset regardless of success or failure and eliminating the inline unset from the sorting callback.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Change is localized to a single method, consisting of a straightforward move of cleanup logic into a finally block and removal from a comparator. Review should verify exception-safety and that state is consistently cleared across paths.

Possibly related PRs

Suggested reviewers

  • abnegate

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Description Check ⚠️ Warning No pull request description was provided by the author. While the check is intentionally lenient and only requires the description to be related to the changeset in some way, the pass criterion explicitly states the description should be "related in some way to the changeset." A missing description provides zero information and fails to establish any connection to the changes made, which does not satisfy this minimum requirement. The author should add a brief description to the pull request explaining the memory leak issue and how the changes fix it. Even a short explanation of the problem and solution would provide context for reviewers and satisfy this check. At minimum, the description should reference the specific issue or provide a brief rationale for the cleanup logic modifications.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The pull request title "fix: stats usage memory leak" is fully related to the main change in the changeset. The raw summary indicates that the changes involve removing temporary cleanup code in a comparator and implementing more robust cleanup logic with a finally block to fix per-project state cleanup in the StatsUsage.php file. The title directly and specifically references both the component (stats usage) and the issue being addressed (memory leak), making it clear and actionable for someone scanning the commit history.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f427c91 and 18371eb.

📒 Files selected for processing (1)
  • src/Appwrite/Platform/Workers/StatsUsage.php (0 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@loks0n loks0n requested a review from Copilot October 22, 2025 16:02
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a memory leak in the stats usage worker by relocating the unset() call that clears processed project data. The memory leak occurred because projects were being unset too early in the sorting comparison function, preventing proper cleanup after database operations completed.

Key Changes:

  • Moved the unset($this->projects[$sequence]) statement from inside the sorting comparison function to after the database upsert operation

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Oct 22, 2025

Security Scan Results for PR

Docker Image Scan Results

Package Version Vulnerability Severity
binutils 2.44-r2 CVE-2025-5244 HIGH
binutils 2.44-r2 CVE-2025-5245 HIGH
libxml2 2.13.8-r0 CVE-2025-49794 CRITICAL
libxml2 2.13.8-r0 CVE-2025-49796 CRITICAL
libxml2 2.13.8-r0 CVE-2025-49795 HIGH
libxml2 2.13.8-r0 CVE-2025-6021 HIGH
pcre2 10.43-r1 CVE-2025-58050 CRITICAL
golang.org/x/crypto v0.31.0 CVE-2025-22869 HIGH
golang.org/x/oauth2 v0.24.0 CVE-2025-22868 HIGH
stdlib 1.22.10 CVE-2025-47907 HIGH

Source Code Scan Results

🎉 No vulnerabilities found!

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/Appwrite/Platform/Workers/StatsUsage.php (1)

487-549: Apply the same memory leak fix to $this->statDocuments.

The TODO comment on line 544 highlights a similar memory leak issue. The $this->statDocuments array accumulates stat documents but is never cleared after writing to logs DB, causing the same memory leak pattern that this PR fixes for $this->projects.

Apply this diff to clear the array after writing:

             $dbForLogs->upsertDocumentsWithIncrease(
                 'stats',
                 'value',
                 $this->statDocuments
             );
             Console::success('Usage logs pushed to Logs DB');
 
-            /**
-             * todo: Do we need to unset $this->statDocuments?
-             */
-
+            $this->statDocuments = [];
         } catch (Throwable $th) {
             Console::error($th->getMessage());
+        } finally {
+            $this->statDocuments = [];
         }
🧹 Nitpick comments (1)
src/Appwrite/Platform/Workers/StatsUsage.php (1)

456-456: Redundant unset - already handled by finally block.

This unset is redundant since the finally block (line 461) always executes and performs the same cleanup. Remove this line to simplify the code.

Apply this diff to remove the redundant line:

                 $dbForProject->upsertDocumentsWithIncrease('stats', 'value', $projectStats['stats']);
-                unset($this->projects[$sequence]);
                 Console::success('Batch successfully written to DB');
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c38d319 and f427c91.

📒 Files selected for processing (1)
  • src/Appwrite/Platform/Workers/StatsUsage.php (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Setup & Build Appwrite Image
  • GitHub Check: Setup & Build Appwrite Image
  • GitHub Check: scan
🔇 Additional comments (1)
src/Appwrite/Platform/Workers/StatsUsage.php (1)

460-462: Excellent addition - ensures cleanup on exception paths.

The finally block guarantees that per-project state is cleared regardless of success or failure, directly addressing the memory leak mentioned in the PR title.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Oct 22, 2025

✨ Benchmark results

  • Requests per second: 1,194
  • Requests with 200 status code: 215,031
  • P99 latency: 0.163931527

⚡ Benchmark Comparison

Metric This PR Latest version
RPS 1,194 961
200 215,031 172,976
P99 0.163931527 0.203394886

});

$dbForProject->upsertDocumentsWithIncrease('stats', 'value', $projectStats['stats']);
unset($this->projects[$sequence]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

but the unset is already done in the finally block below 🧐

@loks0n loks0n force-pushed the fix-stats-usage-memory-leak branch from f427c91 to 18371eb Compare October 22, 2025 16:53
@loks0n loks0n merged commit 887820d into 1.8.x Oct 22, 2025
8 of 9 checks passed
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.

3 participants