framework: [6.x/7.x] broadcast() method causes memory leak

Like the title says, I ran into a discussion about some memory leaks for beyondcode/laravel-websockets here: https://github.com/beyondcode/laravel-websockets/issues/325

The basic truth is that after some testing, I found out that running broadcast(...) will severely increase the memory for a ReactPHP loop, for instance.

To replicate this:

  • create a new Laravel project
  • composer require laravel/websockets:"dev-2.x-memory-leak"
  • copy the migrations: php artisan vendor:publish --provider="BeyondCode\LaravelWebSockets\WebSocketsServiceProvider" --tag="migrations"
  • run them: php artisan migrate
  • get to the root of the project and run php artisan websockets:serve --statistics-interval=1
  • open an artisan server with php artisan serve
  • Go to http://127.0.0.1:8000/laravel-websockets
  • Click on Connect
  • Watch the memory increase in the console that ran websockets:serve

As explained in https://github.com/beyondcode/laravel-websockets/issues/325#issuecomment-676565842 and https://github.com/beyondcode/laravel-websockets/issues/325#issuecomment-677198100, the problem starts from a broadcast() that causes the leakage.

I have tested even the code within the event. Running broadcast() caused the memory leaks.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 18 (17 by maintainers)

Most upvoted comments

What driver is broadcast using? Redis?

If the memory leak is caused by running SQL queries with many bindings, a fix is to set reporting.maximum_number_of_collected_queries to 0 in config/flare.php.

See above.