Skip to content

Unify client lifecycle management across sync/async and single/batch operations #76

@dev-dsp

Description

@dev-dsp

There's an inconsistency in how user-supplied httpx clients are handled depending on which method you call. The README even documents this for queries(), but I think this deserves a second look.

The whole point of allowing client injection is giving users control over connection pooling, auth, timeouts, and lifecycle. When the library accepts a client but then closes it, that contract falls apart.

This results in

  • Connection pooling becoming useless for batch operations. If I want to run several queries() calls against the same endpoint, I can't reuse connections - which is exactly the scenario where pooling would help most.
  • Users having to remember which methods are "safe" with a shared client and which will kill it.
  • Blocking some reasonable patterns - say, a long-running service that keeps a client pool, and dispatches queries through sparqlx. Right now you have to defensively recreate clients or avoid queries()/updates() entirely.

ClientManager already does the right thing in context() and acontext() - it knows whether a client is auto-managed or user-supplied and acts accordingly. That's the correct design. The issue is that SPARQLWrapper.__exit__ and __aexit__ sidestep this and close unconditionally. Then queries()/updates() use async with on a SPARQLWrapper internally, triggering that unconditional close.

Some possible approaches:

  1. Have __exit__/__aexit__ delegate to ClientManager rather than calling close directly, since the manager already has the ownership logic
  2. Have queries()/updates() use ClientManager.acontext() directly instead of going through SPARQLWrapper's context protocol since thhey only need the async client anyway.
  3. Some combination of the above.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions