Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySqliDriver::getResource throws in PHP 8 instead of returning null whe the resource has been closed prior to the call #409

Closed
dakujem opened this issue Dec 10, 2021 · 1 comment · Fixed by #410

Comments

@dakujem
Copy link
Contributor

dakujem commented Dec 10, 2021

Version: 4.2.5
PHP: 8.0, 8.1 (does not happen on PHP 7)

Bug Description

MySqliDriver::getResource() throws an error in PHP 8 and is thus not suppressed by the @ operator.

Fatal error: Uncaught Error: mysqli object is already closed

The problem is when this happens during shutdown with EALL | E_STRICT setting, when the connection service is being destructed.
I'm sharing the resource and it happens to be closed before the driver is destructed.

Steps To Reproduce

$driver = new MySqliDriver( ... );
$driver->getResource()->close();
$driver->getResource();

Expected Behavior

I expect null to be returned instead of an internal error being thrown.

Possible Solution

    public function getResource(): ?\mysqli
    {
        if (!is_resource($this->connection)) {
            return null;
        }
        return @$this->connection->thread_id ? $this->connection : null;
    }

or

   public function getResource(): ?\mysqli
   {
       try {
           return @$this->connection->thread_id ? $this->connection : null;
       } catch (\Error $e) {
           return null;
       }
   }

Let me know which alternative is better and I'll prepare a PR.

@dg
Copy link
Owner

dg commented Dec 11, 2021

I think the first is more consistent with the rest of code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants