Skip to content

Sync Client missing close() / context manager — httpx connection pool leaks #144

Description

@chassing

Problem

The sync Client has no close() method and no context manager support (__enter__/__exit__). The underlying httpx.Client requires an explicit close() to release the TCP connection pool, but since httpx.Client has no __del__, simply deleting the lightkube Client reference does not close the transport.

This means every Client instance leaks open file descriptors until the process exits or the remote end times them out.

The async GenericClient has the same gap — no aclose() or async with support.

Current workaround

We reach through private attributes to close the httpx transport:

# lightkube.Client._client (GenericSyncClient)._client (httpx.Client)
self._client._client._client.close()

This works but is fragile — it will break if lightkube's internal attribute names change.

Proposed fix

Add close() and context manager support to both Client and AsyncClient:

# Sync
class Client:
    def close(self) -> None:
        self._client._client.close()

    def __enter__(self) -> Self:
        return self

    def __exit__(self, *args: object) -> None:
        self.close()

# Async
class AsyncClient:
    async def aclose(self) -> None:
        await self._client._client.aclose()

    async def __aenter__(self) -> Self:
        return self

    async def __aexit__(self, *args: object) -> None:
        await self.aclose()

Environment

  • lightkube 0.21.0
  • httpx 0.28.1
  • Python 3.14

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions