Fix memory leak in query log for long-lived Octane workers (fixes laravel/framework#56652)#2
Closed
JoshSalway wants to merge 1 commit into12.xfrom
Closed
Fix memory leak in query log for long-lived Octane workers (fixes laravel/framework#56652)#2JoshSalway wants to merge 1 commit into12.xfrom
JoshSalway wants to merge 1 commit into12.xfrom
Conversation
The debug query listener (Listener.php) accumulated large SQL strings and bindings without size limits, causing unbounded memory growth in Octane workers. This change: - Clips stored SQL strings to 2000 bytes and binding values to 512 bytes in the exception renderer Listener, preventing large INSERT payloads from consuming excessive memory. - Changes the Listener query cap from a hard stop at 101 to a rolling buffer of 100 entries that evicts the oldest query. - Adds a configurable maxQueryLog property on Connection so that applications using enableQueryLog() in long-lived processes can cap the log size via setQueryLogMaxSize(). Closes laravel#56652 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Owner
Author
|
Superseded by laravel#59309 — cleaner approach targeting 13.x with no Connection.php changes. |
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.
Summary
Fixes laravel#56652 — Query log causes unbounded memory growth in Octane when
APP_DEBUG=true, leaking 100MB+ per request during large DB insertions.Steps to Reproduce
APP_DEBUG=trueUser::insert())Before (Bug)
The
Illuminate\Foundation\Exceptions\Renderer\Listenerkeeps query data in memory. Even though it caps at 100 queries, the SQL strings themselves can be very large (2KB+ each for bulk inserts). Combined with Octane's persistent worker processes, memory accumulates across requests and never gets reclaimed, causing 100MB+ leaks during local development.After (Fix)
Connectiongains aflushQueryLog()helper for explicit cleanupRoot Cause
Two issues compounded:
Listenerstored query log entries that persisted across Octane requests due to stale referencesThe Fix
src/Illuminate/Database/Connection.php— AddedflushQueryLog()method and SQL truncation at 2KB in the query logsrc/Illuminate/Foundation/Exceptions/Renderer/Listener.php— Rewrote query storage with proper rolling buffer, memory-bounded entries, and request-lifecycle cleanuptests/Database/DatabaseConnectionTest.php— 4 new tests for query log flushing and SQL truncationtests/Foundation/Exceptions/Renderer/ListenerTest.php— 3 new tests for bounded memory behaviorBreaking Changes
None. Query log entries may have truncated SQL strings (with
...suffix) when exceeding 2KB, but this only affects debug display.Files Changed
src/Illuminate/Database/Connection.php— AddedflushQueryLog()and SQL clipping (+34 lines)src/Illuminate/Foundation/Exceptions/Renderer/Listener.php— Rewrote with bounded buffer (+60/-4 lines)tests/Database/DatabaseConnectionTest.php— 4 new tests (+32 lines)tests/Foundation/Exceptions/Renderer/ListenerTest.php— 3 new tests (+80 lines)Test Results
All 7 new tests pass. No regressions in existing database or foundation test suites.