phplaravellaravel-8freeze

Laravel 8 - Hangs intermittently every minute or so (Local and Live Server)


This is a problem that's been plaguing me recently. I can't seem to nail down what's causing it, it happens locally and on my live server. Here's the gist of what happens.

I perform (for example) an AJAX call. I can keep doing the same call every second and it happens as fast as you'd expect. But every 30-60 seconds the call will hang for around 30 seconds before kicking back into gear.

Note: this isn't just ajax calls, it happens with fresh page loads just the same. It also happens when accessing a route directly (not via the front end) but it is easier for me to test and show you the following video:

https://www.youtube.com/watch?v=Cg-rd8FAAog

This happens on any request, not just specific ones, it appears completely random.

The server opens the request, hangs for around 30 seconds, then eventually it will close it as seen here:

Screenshot of a artisan Screenshot of debugger 1 Screenshot of debugger 1

EDIT re above image: The second photo of the debugger shows an intensive bit of code that I have yet to streamline. I tried it with a test case simply using the following and I'm getting the exact same 30 second-ish lag, with only one query showing on the debugbar.

public function(Request $request){
    return response()->json([], 200);
}

I've checked on a few specific examples and none of the code executes in the controller function for the entire (roughly) 30 seconds, then, when it decides to, it just executes it all in milliseconds - like it should.

As stated above, this happens on my local machine and my live server.

I've done a composer dump-autoload and php artisan optimize but this makes no difference.

What's confusing is, the site is nice and quick when it's working - but it just randomly hangs.

Does anyone have a clue what's happening here? Have you experienced anything like this before? Is there any more debugging I can do to find out where/why it hangs?

Here is my composer file:

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "php": "^7.3|^8.0",
        "barryvdh/laravel-dompdf": "^2.0",
        "fruitcake/laravel-cors": "^2.0",
        "guzzlehttp/guzzle": "7.4.1",
        "intervention/image": "^2.7",
        "laravel/framework": "^8.65",
        "laravel/sanctum": "^2.11",
        "laravel/socialite": "^5.5",
        "laravel/tinker": "^2.5",
        "laravel/ui": "^3.3",
        "league/flysystem-aws-s3-v3": "^1.0",
        "league/flysystem-cached-adapter": "~1.0",
        "league/flysystem-sftp": "~1.0",
        "maatwebsite/excel": "^3.1"
    },
    "require-dev": {
        "barryvdh/laravel-debugbar": "3.6.6",
        "facade/ignition": "^2.5",
        "fakerphp/faker": "^1.9.1",
        "laravel/sail": "^1.0.1",
        "mockery/mockery": "1.4.4",
        "nunomaduro/collision": "5.10",
        "phpunit/phpunit": "9.5.10"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}


Solution

  • Ok, I seemed to have finally nailed down the issue. The storage/framework/sessions folder had over 220,000 files in it! I cleared all these out and now everything seems fine. I have also amended the the session timeout to stop this happening again.

    Update:

    The session files weren't being overwritten, new ones were being created. Commenting out SessionStart::class from $middleware seems to have solved this issue (left it alone in $middlewareGroups)

    <?php
    
    namespace App\Http;
    
    use Illuminate\Foundation\Http\Kernel as HttpKernel;
    
    class Kernel extends HttpKernel
    {
    
        /**
         * The application's global HTTP middleware stack.
         *
         * These middleware are run during every request to your application.
         *
         * @var array
         */
        protected $middleware = [
            // \Illuminate\Session\Middleware\StartSession::class,  <------ commented this out
            // \App\Http\Middleware\TrustHosts::class,
            \App\Http\Middleware\TrustProxies::class,
            \Fruitcake\Cors\HandleCors::class,
            \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
            \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
            \App\Http\Middleware\TrimStrings::class,
            \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        ];
    
        /**
         * The application's route middleware groups.
         *
         * @var array
         */
        protected $middlewareGroups = [
            'web' => [
                \App\Http\Middleware\EncryptCookies::class,
                \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
                \Illuminate\Session\Middleware\StartSession::class,
                // \Illuminate\Session\Middleware\AuthenticateSession::class,
                \Illuminate\View\Middleware\ShareErrorsFromSession::class,
                \App\Http\Middleware\VerifyCsrfToken::class,
                \Illuminate\Routing\Middleware\SubstituteBindings::class,
            ],
    
            'api' => [
                // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
                'throttle:api',
                \Illuminate\Routing\Middleware\SubstituteBindings::class,
            ],
        ];
    
        /**
         * The application's route middleware.
         *
         * These middleware may be assigned to groups or used individually.
         *
         * @var array
         */
        protected $routeMiddleware = [
            'auth' => \App\Http\Middleware\Authenticate::class,
            'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
            'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
            'can' => \Illuminate\Auth\Middleware\Authorize::class,
            'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
            'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
            'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
            'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
            'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
            'checkPermission' => \App\Http\Middleware\CheckPermission::class,
        ];
    }