From 8e45076ac65870d58e2fe206ca23f3546b60bf03 Mon Sep 17 00:00:00 2001 From: Oleg Kolesov <47299406+oleg1540@users.noreply.github.com> Date: Fri, 6 Jan 2023 07:36:07 +0300 Subject: [PATCH] #53 - cancel query before close connection (#54) --- src/PgSqlHandle.php | 1 + test/AbstractConnectTest.php | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/PgSqlHandle.php b/src/PgSqlHandle.php index fe0ceba..4cfe74a 100644 --- a/src/PgSqlHandle.php +++ b/src/PgSqlHandle.php @@ -195,6 +195,7 @@ private function fetchTypes(string $id): Promise public function close(): void { if ($this->handle instanceof \PgSql\Connection || \is_resource($this->handle)) { + \pg_cancel_query($this->handle); \pg_close($this->handle); $this->handle = null; } diff --git a/test/AbstractConnectTest.php b/test/AbstractConnectTest.php index 93d8b64..27916fe 100644 --- a/test/AbstractConnectTest.php +++ b/test/AbstractConnectTest.php @@ -66,4 +66,16 @@ public function testConnectInvalidUser(): Promise return $this->connect(PostgresConnectionConfig::fromString('host=localhost user=invalid'), new TimeoutCancellationToken(100)); } + + public function testConnectionClose(): \Generator + { + $connection = yield $this->connect(PostgresConnectionConfig::fromString('host=localhost user=postgres')); + $this->assertInstanceOf(Connection::class, $connection); + + $connection->execute('SELECT pg_sleep(10)'); + + $start = microtime(true); + $connection->close(); + $this->assertEquals(0, round(microtime(true) - $start)); + } }