friends-of-reactphp / mysql

Async MySQL database client for ReactPHP.
MIT License
334 stars 66 forks source link

Can not send more queries after 'Not supportted value type' exception #167

Open yyydevf opened 1 year ago

yyydevf commented 1 year ago

Hello

I have a bug in v 0.5. and v0.6.

While we have an invalid type of argument, we haven't drop current state of mysql, look at example, because exception does not catch

<?php

declare(strict_types=1);

require_once 'vendor/autoload.php';

$connection = $factory->createLazyConnection('root:root@127.0.0.1:3306/storage');
$connection->query("update table set value = ?", [new stdClass()]) // Invalid type of object
    ->otherwise(function ($e) use ($connection) {
        return $connection->query("select * from table where id = 1")
            ->then(function ($e) {
                echo 1; // thats never happened
            })
            ->otherwise(function ($e) use ($connection) {
                echo 1; // thats too
            });
    });

Look at this method: https://github.com/friends-of-reactphp/mysql/blob/c36b92f1ede95f49036b6ba1805736f80fa4d4e0/src/Io/Parser.php#L383

And its body (i do not know how to reply multiple lines of code, sorry, so paste my)

protected function nextRequest($isHandshake = false)
    {
        if (!$isHandshake && $this->phase != self::PHASE_HANDSHAKED) {
            return false;
        }

        if ($this->currCommand === null && !$this->executor->isIdle()) {
            $command = $this->executor->dequeue(); 
            $this->currCommand = $command; // current command set here

            if ($command instanceof AuthenticateCommand) {
                $this->phase = self::PHASE_AUTH_SENT;
                $this->sendPacket($command->authenticatePacket($this->scramble, $this->buffer));
            } else {
                $this->seq = 0;
                $this->sendPacket($this->buffer->buildInt1($command->getId()) . $command->getSql()); // Exception occurs here
            }
        }

        return true;
    }

Exception line never catched and just reject: https://github.com/friends-of-reactphp/mysql/blob/89067b22fef892b90670722c20368a647c5b5fc6/src/Io/Query.php#L119 https://github.com/friends-of-reactphp/mysql/blob/89067b22fef892b90670722c20368a647c5b5fc6/src/Io/Connection.php#L72

So we are cannot send more queries from its connection, cause do not drop $this->currCommand

SimonFrings commented 1 year ago

@yyydevf Thanks for reporting this :+1:

This indeed sounds like a bug, seems like we need some additional tests for this case to confirm. If this is a bug we then need some code adjustments. We're always happy about PRs, so if you want to take a closer look at this I'm always happy to help :+1:

SimonFrings commented 1 year ago

I labeled this ticket as a question for now until we're sure this is a bug.

pfk84 commented 1 year ago

I can confirm this is indeed an issue.

pfk84 commented 6 months ago

Any news on this issue? Do you need help fixing this? The connection seems to hang until whatever timeout is reached rapidly exhausting connections until max MySQL connections are reached...

SimonFrings commented 6 months ago

@pfk84 Help with this is always appreciated. In order to properly fix this we'd need a test to reproduce this behavior and then make the failing test green by fixing the reported issue. I looked into this before but didn't really found the cause of this. So I guess all I want to say here is: Pull request are welcome 😉