From c818d5603dbcec920504bf48717adb623204c34b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 27 Aug 2025 09:24:28 +0200 Subject: [PATCH] Garbage collect after every request --- CHANGELOG.md | 17 +++++++++++++++++ bin/roadrunner-worker.php | 8 ++++++++ composer.json | 2 +- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51ab54ac..2e6c1ea3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org). +## [4.5.2] - 2025-08-27 +### Added +* *Nothing* + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#2433](https://github.com/shlinkio/shlink/issues/2433) Try to mitigate memory leaks allowing RoadRunner to garbage collect memory after every request and every job, by setting `GC_COLLECT_CYCLES=true`. + + ## [4.5.1] - 2025-08-24 ### Added * *Nothing* diff --git a/bin/roadrunner-worker.php b/bin/roadrunner-worker.php index 8466c683..a39d6913 100644 --- a/bin/roadrunner-worker.php +++ b/bin/roadrunner-worker.php @@ -2,18 +2,22 @@ declare(strict_types=1); +namespace Shlinkio\Shlink; + use Mezzio\Application; use Psr\Container\ContainerInterface; use Shlinkio\Shlink\Common\Middleware\RequestIdMiddleware; use Shlinkio\Shlink\EventDispatcher\RoadRunner\RoadRunnerTaskConsumerToListener; use Spiral\RoadRunner\Http\PSR7Worker; +use function gc_collect_cycles; use function Shlinkio\Shlink\Config\env; (static function (): void { /** @var ContainerInterface $container */ $container = include __DIR__ . '/../config/container.php'; $rrMode = env('RR_MODE'); + $gcCollectCycles = env('GC_COLLECT_CYCLES', default: false); if ($rrMode === 'http') { // This was spin-up as a web worker @@ -25,6 +29,10 @@ use function Shlinkio\Shlink\Config\env; $worker->respond($app->handle($req)); } catch (Throwable $e) { $worker->getWorker()->error((string) $e); + } finally { + if ($gcCollectCycles) { + gc_collect_cycles(); + } } } } else { diff --git a/composer.json b/composer.json index 915ff2db..6c41d517 100644 --- a/composer.json +++ b/composer.json @@ -45,7 +45,7 @@ "shlinkio/doctrine-specification": "^2.2", "shlinkio/shlink-common": "^7.1", "shlinkio/shlink-config": "^4.0", - "shlinkio/shlink-event-dispatcher": "^4.2", + "shlinkio/shlink-event-dispatcher": "^4.3", "shlinkio/shlink-importer": "^5.6", "shlinkio/shlink-installer": "^9.6", "shlinkio/shlink-ip-geolocation": "^4.3",