Skip to content

Commit 069183d

Browse files
committed
Fix callback accumulation in SqlStatementPool
Fixes amphp/postgres#67.
1 parent 85449d5 commit 069183d

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/SqlCommonConnectionPool.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ abstract class SqlCommonConnectionPool implements SqlConnectionPool
5151

5252
private readonly DeferredFuture $onClose;
5353

54+
/** @var \WeakMap<TStatement, true> */
55+
private \WeakMap $statements;
56+
5457
/**
5558
* Creates a Statement of the appropriate type using the Statement object returned by the Link object and the
5659
* given release callable.
@@ -114,6 +117,10 @@ public function __construct(
114117
}
115118

116119
$this->connections = $connections = new \SplObjectStorage();
120+
121+
/** @var \WeakMap<TStatement, true> For Psalm. */
122+
$this->statements = new \WeakMap();
123+
117124
$this->idle = $idle = new \SplQueue();
118125
$this->onClose = new DeferredFuture();
119126

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

217+
foreach ($this->statements as $statement => $_) {
218+
$statement->close();
219+
}
220+
210221
$this->onClose->complete();
211222

212223
$this->awaitingConnection?->error(new SqlException("Connection pool closed"));
@@ -359,7 +370,12 @@ public function execute(string $sql, array $params = []): SqlResult
359370
public function prepare(string $sql): SqlStatement
360371
{
361372
/** @psalm-suppress InvalidArgument Psalm is not properly detecting the templated return type. */
362-
return $this->createStatementPool($sql, $this->prepareStatement(...));
373+
$statement = $this->createStatementPool($sql, $this->prepareStatement(...));
374+
375+
$this->statements[$statement] = true;
376+
377+
/** @var TStatement $statement Psalm is not properly detecting the templated type. */
378+
return $statement;
363379
}
364380

365381
/**

src/SqlStatementPool.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ public function __construct(SqlConnectionPool $pool, string $sql, \Closure $prep
7979

8080
EventLoop::unreference($timeoutWatcher);
8181
$this->onClose(static fn () => EventLoop::cancel($timeoutWatcher));
82-
83-
$this->pool->onClose(static fn () => $onClose->isComplete() || $onClose->complete());
8482
}
8583

8684
public function __destruct()

0 commit comments

Comments
 (0)