poggit/libasynql

Race condition when creating a connection

dries-c opened this issue · 1 comments

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

Would swapping these two lines fix the issue?

parent::__construct($notifier, $bufferSend, $bufferRecv);
$this->credentials = serialize($credentials);