Skip to content

Cleanup: WebhookSender.__aenter__ eagerly constructs unused httpx.AsyncClient on owned-client path #300

Description

@bokelley

Summary

WebhookSender.__aenter__ calls _get_client() which constructs self._client = httpx.AsyncClient(timeout=...) even when self._owns_client=True. The owned-client path in _send_bytes ignores self._client entirely — it builds a fresh transport-pinned client per request. The eagerly-constructed self._client then sits idle until __aexit__ → aclose() closes it.

Cost

Per async with sender: block: one unused httpx.AsyncClient allocation (a few hundred bytes + an httpcore.AsyncConnectionPool instance). No socket opened (httpx is lazy on first request), so no resource leak.

Not a correctness bug — flagged as cosmetic by python-expert in PR #297 review.

Proposed fix

Skip await self._get_client() in __aenter__ when self._owns_client=True:

async def __aenter__(self) -> WebhookSender:
    if not self._owns_client:
        await self._get_client()  # operator-supplied path still expects this
    return self

aclose() already guards on self._client is not None so the no-op case works.

References

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions