From 04f3f57d4b679b5e3d153719ded21412483d0c90 Mon Sep 17 00:00:00 2001 From: artem Date: Tue, 12 Sep 2023 23:14:56 +0300 Subject: [PATCH 1/4] fix file race condition after view:cache and artisan up --- .../Contracts/Foundation/MaintenanceMode.php | 4 ++++ src/Illuminate/Filesystem/Filesystem.php | 2 ++ .../Foundation/FileBasedMaintenanceMode.php | 3 +++ .../PreventRequestsDuringMaintenance.php | 16 +++++++++++++--- src/Illuminate/View/Compilers/Compiler.php | 15 +++++++++++++-- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Contracts/Foundation/MaintenanceMode.php b/src/Illuminate/Contracts/Foundation/MaintenanceMode.php index 4c948f702d8e..e498a61aa1b8 100644 --- a/src/Illuminate/Contracts/Foundation/MaintenanceMode.php +++ b/src/Illuminate/Contracts/Foundation/MaintenanceMode.php @@ -2,6 +2,8 @@ namespace Illuminate\Contracts\Foundation; +use ErrorException; + interface MaintenanceMode { /** @@ -30,6 +32,8 @@ public function active(): bool; * Get the data array which was provided when the application was placed into maintenance. * * @return array + * + * @throws ErrorException */ public function data(): array; } diff --git a/src/Illuminate/Filesystem/Filesystem.php b/src/Illuminate/Filesystem/Filesystem.php index 2219b15d7a17..9bb6fa9893a5 100644 --- a/src/Illuminate/Filesystem/Filesystem.php +++ b/src/Illuminate/Filesystem/Filesystem.php @@ -483,6 +483,8 @@ public function size($path) * * @param string $path * @return int + * + * @throws ErrorException filemtime - exception after convert PHP errors to ErrorException */ public function lastModified($path) { diff --git a/src/Illuminate/Foundation/FileBasedMaintenanceMode.php b/src/Illuminate/Foundation/FileBasedMaintenanceMode.php index c63522ce34f3..2c4a1c8bb86b 100644 --- a/src/Illuminate/Foundation/FileBasedMaintenanceMode.php +++ b/src/Illuminate/Foundation/FileBasedMaintenanceMode.php @@ -2,6 +2,7 @@ namespace Illuminate\Foundation; +use ErrorException; use Illuminate\Contracts\Foundation\MaintenanceMode as MaintenanceModeContract; class FileBasedMaintenanceMode implements MaintenanceModeContract @@ -46,6 +47,8 @@ public function active(): bool * Get the data array which was provided when the application was placed into maintenance. * * @return array + * + * @throws ErrorException file_get_contents - exception after convert PHP errors to ErrorException */ public function data(): array { diff --git a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php index 7657acf1daf3..ec9b2d29552b 100644 --- a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php +++ b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php @@ -3,6 +3,7 @@ namespace Illuminate\Foundation\Http\Middleware; use Closure; +use ErrorException; use Illuminate\Contracts\Foundation\Application; use Illuminate\Foundation\Http\MaintenanceModeBypassCookie; use Symfony\Component\HttpKernel\Exception\HttpException; @@ -37,11 +38,12 @@ public function __construct(Application $app) /** * Handle an incoming request. * - * @param \Illuminate\Http\Request $request - * @param \Closure $next + * @param \Illuminate\Http\Request $request + * @param \Closure $next * @return mixed * * @throws \Symfony\Component\HttpKernel\Exception\HttpException + * @throws ErrorException */ public function handle($request, Closure $next) { @@ -50,7 +52,15 @@ public function handle($request, Closure $next) } if ($this->app->maintenanceMode()->active()) { - $data = $this->app->maintenanceMode()->data(); + try { + $data = $this->app->maintenanceMode()->data(); + } catch (ErrorException $exception) { + if (! $this->app->maintenanceMode()->active()) { + return $next($request); + } + + throw $exception; + } if (isset($data['secret']) && $request->path() === $data['secret']) { return $this->bypassResponse($data['secret']); diff --git a/src/Illuminate/View/Compilers/Compiler.php b/src/Illuminate/View/Compilers/Compiler.php index ea27917353df..3c37599da202 100755 --- a/src/Illuminate/View/Compilers/Compiler.php +++ b/src/Illuminate/View/Compilers/Compiler.php @@ -2,6 +2,7 @@ namespace Illuminate\View\Compilers; +use ErrorException; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Str; use InvalidArgumentException; @@ -89,6 +90,8 @@ public function getCompiledPath($path) * * @param string $path * @return bool + * + * @throws ErrorException */ public function isExpired($path) { @@ -105,8 +108,16 @@ public function isExpired($path) return true; } - return $this->files->lastModified($path) >= - $this->files->lastModified($compiled); + try { + return $this->files->lastModified($path) >= + $this->files->lastModified($compiled); + } catch (ErrorException $exception) { + if (! $this->files->exists($compiled)) { + return true; + } + + throw $exception; + } } /** From d4c2348f2e107b18b99640e62cd30625a8735f37 Mon Sep 17 00:00:00 2001 From: artem Date: Wed, 13 Sep 2023 01:42:09 +0300 Subject: [PATCH 2/4] revert spaces in docblock --- .../Http/Middleware/PreventRequestsDuringMaintenance.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php index ec9b2d29552b..88b962ccf94c 100644 --- a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php +++ b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php @@ -38,8 +38,8 @@ public function __construct(Application $app) /** * Handle an incoming request. * - * @param \Illuminate\Http\Request $request - * @param \Closure $next + * @param \Illuminate\Http\Request $request + * @param \Closure $next * @return mixed * * @throws \Symfony\Component\HttpKernel\Exception\HttpException From d628c15ab8fa36b5eef559a802a54798da714f29 Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 13 Sep 2023 11:47:47 +0300 Subject: [PATCH 3/4] Update src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php Co-authored-by: Dries Vints --- .../Http/Middleware/PreventRequestsDuringMaintenance.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php index 88b962ccf94c..a25e4c4a4b91 100644 --- a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php +++ b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php @@ -43,7 +43,7 @@ public function __construct(Application $app) * @return mixed * * @throws \Symfony\Component\HttpKernel\Exception\HttpException - * @throws ErrorException + * @throws \ErrorException */ public function handle($request, Closure $next) { From c948552fa32b9ef3466e0cc3ab9f9658877a664b Mon Sep 17 00:00:00 2001 From: artem Date: Wed, 13 Sep 2023 11:53:51 +0300 Subject: [PATCH 4/4] code review changes --- src/Illuminate/Contracts/Foundation/MaintenanceMode.php | 4 ---- src/Illuminate/Filesystem/Filesystem.php | 2 -- src/Illuminate/Foundation/FileBasedMaintenanceMode.php | 3 --- src/Illuminate/View/Compilers/Compiler.php | 2 +- 4 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/Illuminate/Contracts/Foundation/MaintenanceMode.php b/src/Illuminate/Contracts/Foundation/MaintenanceMode.php index e498a61aa1b8..4c948f702d8e 100644 --- a/src/Illuminate/Contracts/Foundation/MaintenanceMode.php +++ b/src/Illuminate/Contracts/Foundation/MaintenanceMode.php @@ -2,8 +2,6 @@ namespace Illuminate\Contracts\Foundation; -use ErrorException; - interface MaintenanceMode { /** @@ -32,8 +30,6 @@ public function active(): bool; * Get the data array which was provided when the application was placed into maintenance. * * @return array - * - * @throws ErrorException */ public function data(): array; } diff --git a/src/Illuminate/Filesystem/Filesystem.php b/src/Illuminate/Filesystem/Filesystem.php index 9bb6fa9893a5..2219b15d7a17 100644 --- a/src/Illuminate/Filesystem/Filesystem.php +++ b/src/Illuminate/Filesystem/Filesystem.php @@ -483,8 +483,6 @@ public function size($path) * * @param string $path * @return int - * - * @throws ErrorException filemtime - exception after convert PHP errors to ErrorException */ public function lastModified($path) { diff --git a/src/Illuminate/Foundation/FileBasedMaintenanceMode.php b/src/Illuminate/Foundation/FileBasedMaintenanceMode.php index 2c4a1c8bb86b..c63522ce34f3 100644 --- a/src/Illuminate/Foundation/FileBasedMaintenanceMode.php +++ b/src/Illuminate/Foundation/FileBasedMaintenanceMode.php @@ -2,7 +2,6 @@ namespace Illuminate\Foundation; -use ErrorException; use Illuminate\Contracts\Foundation\MaintenanceMode as MaintenanceModeContract; class FileBasedMaintenanceMode implements MaintenanceModeContract @@ -47,8 +46,6 @@ public function active(): bool * Get the data array which was provided when the application was placed into maintenance. * * @return array - * - * @throws ErrorException file_get_contents - exception after convert PHP errors to ErrorException */ public function data(): array { diff --git a/src/Illuminate/View/Compilers/Compiler.php b/src/Illuminate/View/Compilers/Compiler.php index 3c37599da202..7ec15ac96f74 100755 --- a/src/Illuminate/View/Compilers/Compiler.php +++ b/src/Illuminate/View/Compilers/Compiler.php @@ -91,7 +91,7 @@ public function getCompiledPath($path) * @param string $path * @return bool * - * @throws ErrorException + * @throws \ErrorException */ public function isExpired($path) {