poggit / libasynql

Asynchronous MySQL access library for PocketMine plugins.
https://poggit.github.io/libasynql
Apache License 2.0
132 stars 44 forks source link

Race condition when creating a connection #32

Closed dries-c closed 4 years ago

dries-c commented 4 years ago

Describe the bug Due to the recent PR @CortexPE made, there's a race condition that can cause the server to crash on startup or when launching another worker.

Sample code Code:

libasynql::create($plugin, array('type' => 'mysql', 'mysql' => ['host' => $credentials[0], 'username' => $credentials[1], 'password' => $credentials[2], 'schema' => $credentials[3]], 'worker-limit' => 4), [ 'mysql' => 'mysql.sql' ]);

Sometimes returns:

Fatal error: Uncaught TypeError: unserialize() expects parameter 1 to be string, null given in phar:///home/plugins/NGEssentials.phar/src/_48ad7499cbf87afcbea6poggit/libasynql/mysqli/MysqliThread.php:72

Stack trace:

1 phar:///home/plugins/NGEssentials.phar/src/_48ad7499cbf87afcbea6poggit/libasynql/base/SqlSlaveThread.php(69): _48ad7499cbf87afcbea6poggit\libasynql\mysqli\MysqliThread->createConn(NULL),#0 phar:///home/plugins/NGEssentials.phar/src/_48ad7499cbf87afcbea6poggit/libasynql/mysqli/MysqliThread.php(72): unserialize(NULL)

2 [internal function]: _48ad7499cbf87afcbea6poggit\libasynql\base\SqlSlaveThread->run()

3 {main} thrown in phar:///home/plugins/NGEssentials.phar/src/_48ad7499cbf87afcbea6poggit/libasynql/mysqli/MysqliThread.php on line 72

Expected behavior No race conditions.

Environment OS: debian:latest PocketMine version: https://github.com/pmmp/PocketMine-MP/releases/tag/3.12.0

SOF3 commented 4 years ago

Would swapping these two lines fix the issue? https://github.com/poggit/libasynql/blob/673c017c0549187f9aa9ac1a8c650c799541cd3f/libasynql/src/poggit/libasynql/mysqli/MysqliThread.php#L66-L67