Skip to content

fix: move const inside class to prevent memory leaks in worker mode#152

Merged
marandaneto merged 3 commits into
PostHog:mainfrom
zip-fa:master
May 30, 2026
Merged

fix: move const inside class to prevent memory leaks in worker mode#152
marandaneto merged 3 commits into
PostHog:mainfrom
zip-fa:master

Conversation

@zip-fa
Copy link
Copy Markdown
Contributor

@zip-fa zip-fa commented May 27, 2026

💡 Motivation and Context

PHP applications running in long-lived worker modes (FrankenPHP, Swoole, RoadRunner, Laravel Octane) suffer from memory leaks caused by namespace-level constants in lib/Client.php and lib/FeatureFlag.php.

When SIZE_LIMIT and LONG_SCALE are declared at the namespace level with const, PHP re-evaluates and allocates them on every request in persistent worker processes, since namespace constants are not garbage-collected between requests in these runtimes. Moving them to private const on their respective classes ties their lifecycle to the class autoloader cache, which is loaded once and reused — eliminating the leak.

💚 How did you test it?

  • Ran a Laravel Octane (FrankenPHP worker mode) application with PostHog SDK under load and monitored memory_get_usage() over 10,000+ requests — memory remained stable after the fix, whereas it grew linearly before.
  • Verified all existing unit tests pass (composer test).
  • Confirmed self::SIZE_LIMIT and self::LONG_SCALE resolve to the same values as the original namespace constants.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

Fixes #153

@zip-fa zip-fa requested a review from a team as a code owner May 27, 2026 16:19
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 27, 2026

Reviews (1): Last reviewed commit: "fix: move const inside class to prevent ..." | Re-trigger Greptile

@marandaneto
Copy link
Copy Markdown
Member

@zip-fa can you add a changeset pls? https://github.com/PostHog/posthog-php/blob/main/RELEASING.md
thansk for the pr

@zip-fa
Copy link
Copy Markdown
Contributor Author

zip-fa commented May 30, 2026

@marandaneto done

@marandaneto marandaneto merged commit e26230c into PostHog:main May 30, 2026
16 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.

Memory leak from namespace-level constants in worker mode

2 participants