Skip to content

background refresh thread cannot be stopped, so discarded instances keep a thread alive and keep logging in #10253

Description

@doernemt

Problem description

Each KeyAuth instance starts a daemon thread (token_refresh_task) that loops forever. There's no way to stop it: no public close(), the loop has no exit condition.
We're using the to-be-released pubsub-sdk-python that inherits this in long-running processes (Airflow Trigger) that create more than one KeyAuth over their lifetime (e.g., key rotation), every discarded instance leaks a thread that keeps requesting tokens.

Proposed solution

Add a public, idempotent close()

def close(self) -> None:
    """Stop the background refresh thread and executor. Safe to call more than once."""
    self._stop.set()
    self.executor.shutdown(wait=False, cancel_futures=True)
    atexit.unregister(self.__shutdown)


Modified loop:
def __start_token_refresh_task(self):
    def token_refresh_task():
        while not self._stop.wait(self.TOKEN_EXPIRY_CHECK_INTERVAL.total_seconds()):

Additional information

Many thanks in advance!

Activity

  1. mittalpk commented on Oct 9, 2026

    @mittalpk

    I'd be happy to send a PR for this if outside contributions are welcome here: an idempotent close() on KeyAuth that stops the refresh loop and shuts down the executor, with tests. Let me know if you'd rather handle it internally or want a different shape, for example context-manager support as well.

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

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions