Spatie/fork - Concurency Broken - V1.2.4 => V1.2.5
Introduction
In this article, we will be discussing a concurrency issue with the Spatie/Fork package in Laravel. Specifically, we will be looking at the differences between version 1.2.4 and 1.2.5 of the package and how it affects the stability of scheduled tasks that utilize concurrency.
Laravel Version and Environment
- Laravel Version: 12.12.0
- PHP Version: 8.3.20
- Database Driver & Version: mariadb 15.1 Distrib 10.6.21-MariaDB, for debian-linux-gnu (x86_64) using EditLine wrapper
Description of the Issue
I am using Spatie/Fork as the driver for concurrency in Laravel. However, I started noticing severe crashes in scheduled tasks that use concurrency, resulting in PDO errors. Initially, I thought the issue was with my code, so I did a lot of refactoring, but the problems persisted.
After downgrading the Spatie/Fork package to version 1.2.4, everything started working as usual. To confirm that this was indeed the issue, I upgraded and downgraded the package again, and the same results occurred. This led me to conclude that the problem was with version 1.2.5 of the package.
v1.2.5 Errors
The errors I encountered with version 1.2.5 of the package were related to SQLSTATE[HY093]: Invalid parameter number. The errors were as follows:
[2025-05-02 15:10:15] production.ERROR: SQLSTATE[HY093]: Invalid parameter number (Connection: mariadb, SQL: select * from `accounts` where exists (select * from `subscriptions` where `accounts`.`user_id` = `subscriptions`.`user_id` and `status` = active and `ends_at` > 2025-05-02 15:10:15)) {"exception":"[object] (Illuminate\\Database\\QueryException(code: HY093): SQLSTATE[HY093]: Invalid parameter number (Connection: mariadb, SQL: select * from `accounts` where exists (select * from `subscriptions` where `accounts`.`user_id` = `subscriptions`.`user_id` and `status` = active and `ends_at` > 2025-05-02 15:10:15))
Another error I encountered was:
SQLSTATE[HY093]: Invalid parameter number (Connection: mysql, SQL: select * from `farmers` where `id` = 77 limit 1)
v1.2.4: The Stable Version
After downgrading to version 1.2.4 of the package, everything went back to normal. This led me to investigate the changes made between version 1.2.4 and 1.2.5.
The Culprit: posix_kill()
After going through the diff, I discovered the culprit behind the issue. The function call posix_kill(getmypid(), SIGKILL);
was introduced in version 1.2.5 of the package.
https://github.com/spatie/fork/blob/0fe5055b39d4043ccfe97e201e76d364424a55/src/Fork.php#L136
This function call is responsible for sending a SIGKILL signal to the current process. However, this can cause issues with PDO and MySQL connections, leading to the errors I encountered.
Steps to Reproduce
To reproduce this issue, simply run queries with bindings inside a background process. This will trigger the errors related to SQLSTATE[HY093]: Invalid parameter number.
Conclusion
Introduction
In our previous article, we discussed a concurrency issue with the Spatie/Fork package in Laravel. Specifically, we looked at the differences between version 1.2.4 and 1.2.5 of the package and how it affects the stability of scheduled tasks that utilize concurrency.
In this article, we will be answering some frequently asked questions related to the Spatie/Fork concurrency issue.
Q: What is the Spatie/Fork package?
A: The Spatie/Fork package is a concurrency driver for Laravel that allows you to run tasks in the background. It provides a simple and efficient way to handle concurrency in your Laravel applications.
Q: What is the issue with Spatie/Fork version 1.2.5?
A: The issue with Spatie/Fork version 1.2.5 is related to the introduction of the posix_kill()
function call. This function call can cause issues with PDO and MySQL connections, leading to errors.
Q: What kind of errors can I expect with Spatie/Fork version 1.2.5?
A: With Spatie/Fork version 1.2.5, you can expect errors related to SQLSTATE[HY093]: Invalid parameter number. These errors can occur when running queries with bindings inside a background process.
Q: How can I reproduce the issue?
A: To reproduce the issue, simply run queries with bindings inside a background process. This will trigger the errors related to SQLSTATE[HY093]: Invalid parameter number.
Q: What is the recommended solution?
A: The recommended solution is to use version 1.2.4 of the Spatie/Fork package until the issue is resolved in a future version of the package.
Q: Can I use a different concurrency driver?
A: Yes, you can use a different concurrency driver. However, keep in mind that the Spatie/Fork package is a popular and widely-used concurrency driver, and switching to a different driver may require significant changes to your code.
Q: Will the issue be fixed in a future version of Spatie/Fork?
A: Yes, the issue will be fixed in a future version of Spatie/Fork. The Spatie team is aware of the issue and is working on a fix.
Q: How can I stay up-to-date with the latest developments on the Spatie/Fork issue?
A: You can stay up-to-date with the latest developments on the Spatie/Fork issue by following the Spatie team on GitHub and keeping an eye on the Spatie/Fork package repository.
Conclusion
In conclusion, the Spatie/Fork concurrency issue is related to the introduction of the posix_kill()
function call in version 1.2.5 of the package. This function call can cause issues with PDO and MySQL connections, leading to errors. The recommended solution is to use version 1.2.4 of the package until the issue is resolved in a future version of the package.