{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/10604","labels_url":"https://api.github.com/repos/laravel/framework/issues/10604/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/10604/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/10604/events","html_url":"https://github.com/laravel/framework/issues/10604","id":111496383,"number":10604,"title":"Queue worker daemon loses connection when unserializing model","user":{"login":"REBELinBLUE","id":2143908,"avatar_url":"https://avatars.githubusercontent.com/u/2143908?v=3","gravatar_id":"","url":"https://api.github.com/users/REBELinBLUE","html_url":"https://github.com/REBELinBLUE","followers_url":"https://api.github.com/users/REBELinBLUE/followers","following_url":"https://api.github.com/users/REBELinBLUE/following{/other_user}","gists_url":"https://api.github.com/users/REBELinBLUE/gists{/gist_id}","starred_url":"https://api.github.com/users/REBELinBLUE/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/REBELinBLUE/subscriptions","organizations_url":"https://api.github.com/users/REBELinBLUE/orgs","repos_url":"https://api.github.com/users/REBELinBLUE/repos","events_url":"https://api.github.com/users/REBELinBLUE/events{/privacy}","received_events_url":"https://api.github.com/users/REBELinBLUE/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-10-14T21:22:48Z","updated_at":"2015-10-14T23:22:26Z","closed_at":null,"body":"This only seems to happen when using MariaDB and the mysqlnd driver\r\n\r\nWhen the queue worker is running in daemon mode and it takes a job from the queue which has a model passed into the constructor the following error occurs\r\n\r\n```\r\n[2015-10-14 21:03:42] local.ERROR: exception 'ErrorException' with message 'Error while sending STMT_PREPARE packet. PID=11613' in /var/www/test/vendor/laravel/framework/src/Illuminate/Database/Connection.php:319\r\nStack trace:\r\n#0 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'Error while sen...', '/var/www/test/v...', 319, Array)\r\n#1 /var/www/test/vendor/laravel/framework/src/Illuminate/Database/Connection.php(319): PDO->prepare('select * from `...')\r\n#2 /var/www/test/vendor/laravel/framework/src/Illuminate/Database/Connection.php(644): Illuminate\\Database\\Connection->Illuminate\\Database\\{closure}(Object(Illuminate\\Database\\MySqlConnection), 'select * from `...', Array)\r\n#3 /var/www/test/vendor/laravel/framework/src/Illuminate/Database/Connection.php(611): Illuminate\\Database\\Connection->runQueryCallback('select * from `...', Array, Object(Closure))\r\n#4 /var/www/test/vendor/laravel/framework/src/Illuminate/Database/Connection.php(324): Illuminate\\Database\\Connection->run('select * from `...', Array, Object(Closure))\r\n#5 /var/www/test/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1406): Illuminate\\Database\\Connection->select('select * from `...', Array, true)\r\n#6 /var/www/test/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1383): Illuminate\\Database\\Query\\Builder->runSelect()\r\n#7 /var/www/test/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(409): Illuminate\\Database\\Query\\Builder->get(Array)\r\n#8 /var/www/test/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(168): Illuminate\\Database\\Eloquent\\Builder->getModels(Array)\r\n#9 /var/www/test/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(140): Illuminate\\Database\\Eloquent\\Builder->get(Array)\r\n#10 /var/www/test/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(87): Illuminate\\Database\\Eloquent\\Builder->first(Array)\r\n#11 /var/www/test/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(119): Illuminate\\Database\\Eloquent\\Builder->find(25, Array)\r\n#12 [internal function]: Illuminate\\Database\\Eloquent\\Builder->findOrFail(25)\r\n#13 /var/www/test/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(3459): call_user_func_array(Array, Array)\r\n#14 /var/www/test/vendor/laravel/framework/src/Illuminate/Queue/SerializesModels.php(65): Illuminate\\Database\\Eloquent\\Model->__call('findOrFail', Array)\r\n#15 /var/www/test/vendor/laravel/framework/src/Illuminate/Queue/SerializesModels.php(65): App\\Post->findOrFail(25)\r\n#16 /var/www/test/vendor/laravel/framework/src/Illuminate/Queue/SerializesModels.php(40): App\\Jobs\\QueuePost->getRestoredPropertyValue(Object(Illuminate\\Contracts\\Database\\ModelIdentifier))\r\n#17 [internal function]: App\\Jobs\\QueuePost->__wakeup()\r\n#18 /var/www/test/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(38): unserialize('O:18:\"App\\\\Jobs\\\\...')\r\n#19 /var/www/test/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(129): Illuminate\\Queue\\CallQueuedHandler->call(Object(Illuminate\\Queue\\Jobs\\BeanstalkdJob), Array)\r\n#20 /var/www/test/vendor/laravel/framework/src/Illuminate/Queue/Jobs/BeanstalkdJob.php(53): Illuminate\\Queue\\Jobs\\Job->resolveAndFire(Array)\r\n#21 /var/www/test/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(208): Illuminate\\Queue\\Jobs\\BeanstalkdJob->fire()\r\n#22 /var/www/test/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(159): Illuminate\\Queue\\Worker->process('beanstalkd', Object(Illuminate\\Queue\\Jobs\\BeanstalkdJob), '1', 0)\r\n#23 /var/www/test/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(111): Illuminate\\Queue\\Worker->pop(NULL, 'default', 0, '3', '1')\r\n#24 /var/www/test/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(87): Illuminate\\Queue\\Worker->runNextJobForDaemon(NULL, 'default', 0, '3', '1')\r\n#25 /var/www/test/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(103): Illuminate\\Queue\\Worker->daemon(NULL, 'default', 0, 128, '3', '1')\r\n#26 /var/www/test/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(71): Illuminate\\Queue\\Console\\WorkCommand->runWorker(NULL, 'default', 0, 128, true)\r\n#27 [internal function]: Illuminate\\Queue\\Console\\WorkCommand->fire()\r\n#28 /var/www/test/vendor/laravel/framework/src/Illuminate/Container/Container.php(503): call_user_func_array(Array, Array)\r\n#29 /var/www/test/vendor/laravel/framework/src/Illuminate/Console/Command.php(150): Illuminate\\Container\\Container->call(Array)\r\n#30 /var/www/test/vendor/symfony/console/Command/Command.php(259): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))\r\n#31 /var/www/test/vendor/laravel/framework/src/Illuminate/Console/Command.php(136): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))\r\n#32 /var/www/test/vendor/symfony/console/Application.php(878): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))\r\n#33 /var/www/test/vendor/symfony/console/Application.php(195): Symfony\\Component\\Console\\Application->doRunCommand(Object(Illuminate\\Queue\\Console\\WorkCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))\r\n#34 /var/www/test/vendor/symfony/console/Application.php(126): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))\r\n#35 /var/www/test/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(107): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))\r\n#36 /var/www/test/artisan(36): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))\r\n#37 {main}\r\n```\r\n\r\nAs you can see, this is happening inside Laravel's automatic call to findOrFail when getting a job from the queue\r\n\r\nAdding the following to the model seems to fix it\r\n\r\n```\r\n\r\n    public function findOrFail()\r\n    {\r\n        if (App::runningInConsole()) {\r\n            DB::reconnect();\r\n        }\r\n\r\n        return parent::__call('findOrFail', func_get_args());\r\n    }\r\n```\r\n\r\nbut from what I can see, Laravel is supposed to try and reconnect automatically, and does appear to with the normal mysql server\r\n\r\nYou can see this in action by cloning this repo\r\n\r\nhttps://github.com/REBELinBLUE/laravel-queue-test\r\n\r\nrunning `vagrant up && vagrant ssh` (it uses laravel/homestead) then in the vm `sudo /vagrant/provision/provision.sh` to remove MySQL and install maria, run the migrations and start the queue worker etc\r\n\r\nthen start tailing the log `tail -f /var/www/test/storage/logs/laravel.log`\r\n\r\nLeave it running for a while and visit http://192.168.10.200/ to add a Post model to the queue, you can see beanstalk console at http://192.168.10.200:81/\r\n\r\nYou will eventually see this error in the log. Obviously by default the DB has a wait_timeout of an hour and default interactive_timeout of several hours so you will want to edit `/etc/mysql/my.cnf` and set them quite low (I used 30 seconds in my testing). Restart mysql and queue listener `service restart mysql` and `service restart supervisor`.\r\n\r\nWait until the time limit and then visit the URL to add the queue to the job\r\n\r\nHope that explains it clear enough"},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/148231202","html_url":"https://github.com/laravel/framework/issues/10604#issuecomment-148231202","issue_url":"https://api.github.com/repos/laravel/framework/issues/10604","id":148231202,"user":{"login":"deefour","id":14762,"avatar_url":"https://avatars.githubusercontent.com/u/14762?v=3","gravatar_id":"","url":"https://api.github.com/users/deefour","html_url":"https://github.com/deefour","followers_url":"https://api.github.com/users/deefour/followers","following_url":"https://api.github.com/users/deefour/following{/other_user}","gists_url":"https://api.github.com/users/deefour/gists{/gist_id}","starred_url":"https://api.github.com/users/deefour/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/deefour/subscriptions","organizations_url":"https://api.github.com/users/deefour/orgs","repos_url":"https://api.github.com/users/deefour/repos","events_url":"https://api.github.com/users/deefour/events{/privacy}","received_events_url":"https://api.github.com/users/deefour/received_events","type":"User","site_admin":false},"created_at":"2015-10-14T23:22:26Z","updated_at":"2015-10-14T23:22:26Z","body":"I have a worker that does nothing but process a single, very heavy job that runs once every 24hours. I believe the default 8hr `wait_timeout` is my problem after my latest research. I'm seeing this issue on that worker, also using mariadb and mysqlnd. I don't think this is a Laravel issue though. \r\n\r\nI plan to raise the [`wait_timeout`](https://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_wait_timeout). I will also be adding a manual reconnect to that job when it gets started."}}