GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Feature Request: allow clients to act as context manager and add close() method to cleanup resources · Issue #575 · googleapis/gapic-generator-python

This repository was archived by the owner on Aug 25, 2026. It is now read-only.
This repository was archived by the owner on Aug 25, 2026. It is now read-only.

Feature Request: allow clients to act as context manager and add close() method to cleanup resources #575

Copy link
Copy link
Closed
#987
Closed
Feature Request: allow clients to act as context manager and add close() method to cleanup resources#575
#987
Copy link
Labels
generatorBugs, features, and so forth pertaining to the generated client surfacepriority: p2Moderately-important priority. Fix may not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Description

@tswast
tswast
opened on Aug 18, 2020
Issue body actions

Since a transport object may be passed into a client constructor, the client does not close that transport in when the client object is deleted. This results in leaking socket resources in programs that create client objects dynamically.

Proposed design:

Add the following two ways to explicitly clean-up sockets owned by the client: (1, preferred) allow clients to act as a context manager and (2) a close() method.

  1. Context manager

    Allow all client objects to be used as a context manager. Call close() on the client in the exit method to clean up sockets owned by the client.

    In code samples with short-lived clients, this is the cleanup method we should recommend.

Example:

from google.cloud import bigquery_datatransfer_v1

with bigquery_datatransfer_v1.DataTransferServiceClient() as client:
    print(client.get_transfer_run('some run id').state)
  1. close() method

    Add a close() method to all client objects. When called, clean up sockets owned by the client, including the transport channel.

    Example:

from google.cloud import bigquery_datatransfer_v1

client = bigquery_datatransfer_v1.DataTransferServiceClient()
print(client.get_transfer_run('some run id').state)
client.close()  # clean up any open sockets

References:

  • go/closable-clients-python (Google internal)
  • [BigQuery Data Transfer Service, all GAPIC clients]: provide a close() method and context manager to clean up client-owned resources google-cloud-python#9457
  • fix(bigquery): add close() method to client for releasing open sockets google-cloud-python#9894
  • Connections stay in CLOSE_WAIT state on using kms_v1.KeyManagementServiceClient python-kms#2
Reacted by Aarni Koskela

Activity

  1. tswast commented on Aug 18, 2020

    @tswast
    tswast
    on Aug 18, 2020
    Author
    More actions

    Note: this issue is even more important in the microgenerator, as the transport property now appears to be private.

  2. software-dov commented on Aug 18, 2020

    @software-dov
    software-dov
    on Aug 18, 2020
    Contributor
    More actions

    This is made slightly more complicated because of ownership. Because it's an injected parameter, we can't be sure that the client has the sole reference to the transport; we may wind up closing a transport that other clients are still using. This kind of a corner case, though, and it can probably be ameliorated with documentation and warnings to USE AT YOUR OWN RISK.
    I agree, in principle, that having both context management and an explicit close method` are the right choice, but I'd like to spend a little more time mulling details.

    Reacted by Tim Sweña (Swast)
  3. software-dov
    added
    generatorBugs, features, and so forth pertaining to the generated client surface
    priority: p2Moderately-important priority. Fix may not be included in next release.
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    on Aug 18, 2020
  4. busunkim96
    added a commit that references this issue on Sep 23, 2020

    fix: add method to close httplib2 connections (#1038)

    98888da
  5. gcf-merge-on-green
    added a commit that references this issue on Sep 23, 2020

    chore: release 1.12.2 (#1044)

    2d076b8
  6. tswast commented on Sep 29, 2020

    @tswast
    tswast
    on Sep 29, 2020
    Author
    More actions

    To cross-link a bit, this / change in transport is affecting the BigQuery client library. It's a case where the BigQuery client library has ownership of a BigQuery Storage client library that it creates, so we must close it when we're done to avoid leaking system resources.

    We're currently working around this issue in googleapis/python-bigquery#278 by calling

    self._bqstorage_client._transport.grpc_channel.close()
    

    when done with the BigQuery Storage client.

  7. software-dov commented on Sep 29, 2020

    @software-dov
    software-dov
    on Sep 29, 2020
    Contributor
    More actions

    Do we need to bump the priority of this feature?

  8. tseaver commented on Jan 13, 2021

    @tseaver
    tseaver
    on Jan 13, 2021
    Contributor
    More actions

    Can we just add close, and then document using contextlib.closing as the way to do the cleanup automatically, including a note on the problematic passed-in-transport case?

  9. atulep
    linked a pull request that will close this issuefeat: add support for context manager in client #987on Sep 7, 2021
  10. atulep
    closed this as completedin #987on Oct 4, 2021
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

    generatorBugs, features, and so forth pertaining to the generated client surfacepriority: p2Moderately-important priority. Fix may not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.