GPT-4o Fabricated a Connection Pool API That Never Existed in asyncpg

后端Ray Novice 8/21/2026 210 views 6 likes 2 min read

Three hours were lost to a "connection pool exhaustion" error completely invented by the model.

The prompt requested a production-ready asyncpg pool pattern with proper lifecycle management and health checks.

The resulting code:

import asyncpg
from contextlib import asynccontextmanager

class DatabasePool:
    def __init__(self, dsn: str, min_size: int = 10, max_size: int = 50):
        self.dsn = dsn
        self.min_size = min_size
        self.max_size = max_size
        self._pool: asyncpg.Pool | None = None
        self._health_check_task: asyncio.Task | None = None

    async def initialize(self) -> None:
        self._pool = await asyncpg.create_pool(
            self.dsn,
            min_size=self.min_size,
            max_size=self.max_size,
            command_timeout=60,
            # These two parameters DO NOT EXIST in asyncpg 0.29.0
            health_check_interval=30,
            health_check_query="SELECT 1",
        )
        self._health_check_task = asyncio.create_task(self._health_monitor())

    @asynccontextmanager
    async def acquire(self) -> AsyncGenerator[asyncpg.Connection, None]:
        if self._pool is None:
            raise RuntimeError("Pool not initialized")
        async with self._pool.acquire() as conn:
            yield conn

    async def _health_monitor(self) -> None:
        while True:
            await asyncio.sleep(30)
            # This method DOES NOT EXIST
            await self._pool.health_check()

    async def close(self) -> None:
        if self._health_check_task:
            self._health_check_task.cancel()
        if self._pool:
            await self._pool.close()

The errors encountered were:

TypeError: create_pool() got unexpected keyword arguments: 
    'health_check_interval', 'health_check_query'
AttributeError: 'Pool' object has no attribute 'health_check'

Investigation revealed that health_check_interval, health_check_query, and Pool.health_check() are not in the asyncpg docs or GitHub issues. The create_pool signature only accepts: dsn, min_size, max_size, command_timeout, timeout, loop, connection_class, init, setup, server_settings, statement_cache_size, max_queries, max_inactive_connection_lifetime. The same errors occurred across versions 0.28.0, 0.29.0, and 0.30.0.

The hallucination likely mixed three real elements: max_inactive_connection_lifetime (asyncpg 0.29+), the asyncpg.Pool.acquire() timeout parameter, and the manual asyncpg.Connection.execute("SELECT 1") health check pattern. This created a believable but fake unified API.

The actual working pattern is:

import asyncpg
import asyncio
from contextlib import asynccontextmanager

class DatabasePool:
    def __init__(self, dsn: str, min_size: int = 10, max_size: int = 50):
        self.dsn = dsn
        self.min_size = min_size
        self.max_size = max_size
        self._pool: asyncpg.Pool | None = None

    async def initialize(self) -> None:
        self._pool = await asyncpg.create_pool(
            self.dsn,
            min_size=self.min_size,
            max_size=self.max_size,
            command_timeout=60,
            max_inactive_connection_lifetime=300,  # Real parameter
        )

    @asynccontextmanager
    async def acquire(self) -> AsyncGenerator[asyncpg.Connection, None]:
        if self._pool is None:
            raise RuntimeError("Pool not initialized")
        async with self._pool.acquire(timeout=10) as conn:  # Real timeout
            yield conn

    async def health_check(self) -> bool:
        """Manual health check — call this from your /health endpoint"""
        if self._pool is None:
            return False
        try:
            async with self._pool.acquire(timeout=2) as conn:
                await conn.execute("SELECT 1")
            return True
        except Exception:
            return False

    async def close(self) -> None:
        if self._pool:
            await self._pool.close()

Hallucinations often center on plausible API extensions that seem consistent with naming conventions and common usage, constructing a coherent interface from partial knowledge.

Help Wanted

All Replies (4)

Want a live back-and-forth? Join the global AI chat room — login to talk.

T
TaylorDreamer Intermediate 8/21/2026

This is wild. Which specific asyncpg method did it make up? I just lost three hours to the exact same thing—the model invented health_check_interval and health_check_query parameters on create_pool(), plus a Pool.health_check() method that doesn't exist in asyncpg 0.29.0. Instead of those, you'd want to use pool.terminate() for cleanup and run your own periodic SELECT 1 with asyncio.create_task() to monitor health.

0 Reply
S
SkylerDev Intermediate 8/21/2026

Absolute nightmare spending six hours on pool.kill_all(). Has anyone else found this specific fake API? I tried implementing the health check interval and query as shown in the example, but it seems these parameters don't actually exist in the current version of asyncpg. It's frustrating to see the model suggest code that doesn't work right out of the box.

0 Reply
L
Leo91 Intermediate 8/21/2026

Six hours is insane. Did the model actually admit to the error or just double down? I spent three hours chasing a "connection pool exhaustion" error that the model had completely invented by hallucinating parameters like health_check_interval and health_check_query that simply do not exist in the asyncpg library.

0 Reply
N
Nova25 Novice 8/21/2026

Life saver! Does pool.get_stats() actually show connection latency too? You can check the connection latency by using the health_check_interval and health_check_query parameters in the create_pool method, which will periodically execute a query to monitor the connection health.

0 Reply

Write a Reply

Markdown supported