laravellaravel-horizon

Laravel Horizon: ErrorException: Warning: PDO::prepare(): MySQL server has gone away


I am struggling with a bug on my production server using Horizon.

ErrorException: Warning: PDO::prepare(): MySQL server has gone away [internal] in unserialize

You can see a stack trace of the error here: https://sentry.io/share/issue/b105b7946b524a9e841f56f44445ea14/

As far as I can tell, this error should be caught by the Laravel framework. I'm not sure why it's not being caught and turned into a QueryException which would then trigger the reconnection and/or killing the worker.

See: https://github.com/laravel/framework/blob/9fb420cc29a7dd5de5051f09c523ffc3ea01b969/src/Illuminate/Database/Connection.php#L663

And then: https://github.com/laravel/framework/blob/9fb420cc29a7dd5de5051f09c523ffc3ea01b969/src/Illuminate/Database/Connection.php#L735

My understanding is that any Exception should be caught and then re-thrown as a QueryException, which would then be properly caught by the framework and then reconnected to the database.

This is an occasional error so it's difficult to reproduce; I've tried to manually throw a similar error but it is caught properly and handled properly.

Any general guidance on why this error might be different in production and ideas on how I can isolate the error would be appreciated.


Solution

  • In case anyone else runs into this, the current theory is that Sentry is catching errors that are still being handled properly by the framework.

    Essentially, the job still completes correctly, because MySQL connection errors are handled automatically by the framework. However, Sentry still catches an error in that error handling process, though the reason is currently unknown.

    For reference, see this discussion on Github:

    https://github.com/laravel/horizon/issues/583