Skip to content

Commit

Permalink
Fix callback accumulation in SqlStatementPool
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Oct 27, 2024
1 parent 85449d5 commit 069183d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/SqlCommonConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ abstract class SqlCommonConnectionPool implements SqlConnectionPool

private readonly DeferredFuture $onClose;

/** @var \WeakMap<TStatement, true> */
private \WeakMap $statements;

/**
* Creates a Statement of the appropriate type using the Statement object returned by the Link object and the
* given release callable.
Expand Down Expand Up @@ -114,6 +117,10 @@ public function __construct(
}

$this->connections = $connections = new \SplObjectStorage();

/** @var \WeakMap<TStatement, true> For Psalm. */
$this->statements = new \WeakMap();

$this->idle = $idle = new \SplQueue();
$this->onClose = new DeferredFuture();

Expand Down Expand Up @@ -207,6 +214,10 @@ public function close(): void
async(fn () => $connection->close())->ignore();
}

foreach ($this->statements as $statement => $_) {
$statement->close();
}

$this->onClose->complete();

$this->awaitingConnection?->error(new SqlException("Connection pool closed"));
Expand Down Expand Up @@ -359,7 +370,12 @@ public function execute(string $sql, array $params = []): SqlResult
public function prepare(string $sql): SqlStatement
{
/** @psalm-suppress InvalidArgument Psalm is not properly detecting the templated return type. */
return $this->createStatementPool($sql, $this->prepareStatement(...));
$statement = $this->createStatementPool($sql, $this->prepareStatement(...));

$this->statements[$statement] = true;

/** @var TStatement $statement Psalm is not properly detecting the templated type. */
return $statement;
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/SqlStatementPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ public function __construct(SqlConnectionPool $pool, string $sql, \Closure $prep

EventLoop::unreference($timeoutWatcher);
$this->onClose(static fn () => EventLoop::cancel($timeoutWatcher));

$this->pool->onClose(static fn () => $onClose->isComplete() || $onClose->complete());
}

public function __destruct()
Expand Down

0 comments on commit 069183d

Please sign in to comment.