Skip to content

Underlying HTTPX clients are shared among several GitHub instances #79

Closed
@frankie567

Description

@frankie567

I discovered a strange behavior while testing out version v0.11.0a2. It seems that if you create several GitHub client in the same process/thread, they reuse the underlying HTTPX client, including the auth flow.

It's a problem if you need to have several clients with different kind of authentications running in your process.

Reproducible example

from githubkit import GitHub, TokenAuthStrategy, UnauthAuthStrategy


def multiple_clients() -> None:
    github_unauth = GitHub(UnauthAuthStrategy())
    response_unauth = github_unauth.rest.rate_limit.get()
    authorization_unauth = response_unauth.headers.get("authorization")
    print(authorization_unauth)  # None, as expected

    github_token = GitHub(TokenAuthStrategy("MY_TOKEN"))
    response_token = github_token.rest.rate_limit.get()
    authorization_token = response_token.headers.get("authorization")
    print(authorization_token)  # Also None, but should be `token MY TOKEN`


if __name__ == "__main__":
    multiple_clients()

Additional context

I suspect it's because ContextVar is used to retain the HTTPX client:

githubkit/githubkit/core.py

Lines 147 to 152 in 8a381cb

self.__sync_client: ContextVar[Optional[httpx.Client]] = ContextVar(
"sync_client", default=None
)
self.__async_client: ContextVar[Optional[httpx.AsyncClient]] = ContextVar(
"async_client", default=None
)


However, what I can't explain is that the same code works correctly in version 0.10.7, while ContextVar were already there. Maybe a side effect of d005552?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions