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 — [BigQuery Data Transfer Service, all GAPIC clients]: provide a close() method and context manager to clean up client-owned resources · Issue #9457 · googleapis/google-cloud-python

[BigQuery Data Transfer Service, all GAPIC clients]: provide a close() method and context manager to clean up client-owned resources #9457

New issue
Copy link
New issue
Copy link
Closed
Closed
[BigQuery Data Transfer Service, all GAPIC clients]: provide a close() method and context manager to clean up client-owned resources#9457
Copy link
Assignees
partheabusunkim96
Labels
api: bigquerydatatransferIssues related to the BigQuery Data Transfer Service API.api: coretype: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Description

@yelsayd
yelsayd
opened on Oct 11, 2019
Issue body actions

Environment details

pip list output:

cachetools                         3.1.1    
certifi                            2019.9.11
chardet                            3.0.4    
google-api-core                    1.14.3   
google-api-python-client           1.7.11   
google-auth                        1.6.3    
google-auth-httplib2               0.0.3    
google-cloud-bigquery              1.20.0   
google-cloud-bigquery-datatransfer 0.4.1    
google-cloud-core                  1.0.3    
google-cloud-pubsub                1.0.2    
google-resumable-media             0.4.1    
googleapis-common-protos           1.6.0    
grpc-google-iam-v1                 0.12.3   
grpcio                             1.24.1   
httplib2                           0.14.0   
idna                               2.8      
pip                                19.2.3   
protobuf                           3.10.0   
pyasn1                             0.4.7    
pyasn1-modules                     0.2.6    
pytz                               2019.3   
requests                           2.22.0   
rsa                                4.0      
setuptools                         41.2.0   
six                                1.12.0   
uritemplate                        3.0.0    
urllib3                            1.25.6   
wheel                              0.33.6   

OS: Ubuntu
Python 3.5.2
API: BigQuery Data Transfer Service

Steps to reproduce

Initialize more than one data transfer client.

Code example

from google.cloud import bigquery_datatransfer_v1
import time

while True:
  dts_client = bigquery_datatransfer_v1.DataTransferServiceClient()
  try:
    dts_client.get_transfer_run('some run id').state
  except:
    print('error!')

  time.sleep(2)

Output of ll /proc/<pid>/fd after three seconds:
0 1 10 11 2 3 4 5 6 7 8 9
And keeps growing.

There's also no way to clean up the client as it doesn't implement __exit__.

Activity

Next
  1. yoshi-automation
    added
    triage meI really want to be triaged.
    on Oct 12, 2019
  2. busunkim96
    changed the title [-]Initializing transfer service client leaks two file descriptors[/-] [+]BigQuery Data Transfer: Initializing transfer service client leaks two file descriptors[/+] on Oct 14, 2019
  3. busunkim96
    added
    api: bigquerydatatransferIssues related to the BigQuery Data Transfer Service API.
    priority: p2Moderately-important priority. Fix may not be included in next release.
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    and removed
    triage meI really want to be triaged.
    on Oct 14, 2019
  4. busunkim96
    assigned
    tswast
    on Oct 14, 2019
  5. tswast commented on Oct 14, 2019

    @tswast
    tswast
    on Oct 14, 2019
    Contributor
    More actions

    This client is 100% auto-generated via our gRPC toolchain. I'll try to summon some of our folks who work at those lower levels to investigate.

  6. busunkim96 commented on Oct 15, 2019

    @busunkim96
    busunkim96
    on Oct 15, 2019
    Contributor
    More actions

    @yebrahim This is unrelated, but you only need to create the dts_client once.

    from google.cloud import bigquery_datatransfer_v1
    import time
    
    dts_client = bigquery_datatransfer_v1.DataTransferServiceClient()
    while True:
      try:
        dts_client.get_transfer_run('some run id').state
      except:
        print('error!')
    
      time.sleep(2)
  7. yelsayd commented on Oct 15, 2019

    @yelsayd
    yelsayd
    on Oct 15, 2019
    Author
    More actions

    Thank you both.
    @busunkim96 indeed unrelated. My specific use case required creating the client on the fly using some parameters that I receive as method args. The example above is a simplification that only reproduces the problem.

    Reacted by Bu Sun Kim and Tim Sweña (Swast)
  8. tseaver commented on Oct 15, 2019

    @tseaver
    tseaver
    on Oct 15, 2019
    Contributor
    More actions

    This isn't a "leak", per se: every GAPIC client has a transport attribute, which in turn holds a gRPC channel as its _channel attribute: each channel holds two open sockets.

    There's also no way to clean up the client as it doesn't implement exit.

    __exit__ is part of Python's context manager protocol, designed for use via the with statement: our clients don't fit that usage. You can, if desired, manually close the channel, e.g., client.transport.channel.close().

  9. tseaver
    added
    type: questionRequest for information or clarification. Not an issue.
    and removed
    priority: p2Moderately-important priority. Fix may not be included in next release.
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    on Oct 15, 2019
  10. yelsayd commented on Oct 15, 2019

    @yelsayd
    yelsayd
    on Oct 15, 2019
    Author
    More actions

    @tseaver:

    • Why doesn't the client fit the usage? Is it not Pythonic to encapsulate client libraries calls within a Python context?
    • If I do close the transport channel manually, can the client be reused afterwards or should I then del and recreate it if needed?
  11. 3 remaining items

  12. yelsayd commented on Nov 17, 2019

    @yelsayd
    yelsayd
    on Nov 17, 2019
    Author
    More actions

    Does it make sense to wrap this call in an API method? Something like client.close()?

  13. tswast
    added
    api: core
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    and removed
    type: questionRequest for information or clarification. Not an issue.
    on Nov 27, 2019
  14. tswast
    changed the title [-]BigQuery Data Transfer: Initializing transfer service client leaks two file descriptors[/-] [+][BigQuery Data Transfer Service, all GAPIC clients]: provide a close() method and context manager to clean up client-owned resources[/+] on Nov 27, 2019
  15. tswast commented on Nov 27, 2019

    @tswast
    tswast
    on Nov 27, 2019
    Contributor
    More actions

    We have just added such a method in the main BigQuery client. Personally, I agree that this makes sense to do across all our client libraries. I've drafted an internal proposal to be reviewed by our core libraries team.

  16. tswast
    assigned
    busunkim96
    and unassigned
    tswast
    on Jan 8, 2020
  17. tswast commented on Jan 8, 2020

    @tswast
    tswast
    on Jan 8, 2020
    Contributor
    More actions

    Googlers: see approved proposal to update generated clients at go/closable-clients-python

    As discussed offline, since this requires changes to the generator, this feature will wait until after the new Python 3-only generator migration is underway.

  18. b-long commented on Feb 8, 2020

    @b-long
    b-long
    on Feb 8, 2020
    More actions

    I'm only asking this in terms of viability, regarding @tseaver 's comment :

    exit is part of Python's context manager protocol, designed for use via the with statement: our clients don't fit that usage

    Is there anything wrong with this approach?

    from google.cloud import bigquery
    from google.cloud.bigquery.dbapi import Connection
    from google.cloud.bigquery.dbapi.cursor import Cursor
    
    class Pep249Cursor(Cursor):
        def __init__(self, client):
            super(Pep249Cursor, self).__init__(client)
    
        def __enter__(self):
            return self
    
        def __exit__(self, exc_type, exc_val, exc_tb):
            self.close()
    
    
    def create_tbl_if_not_exists(c):
        c.execute("CREATE TABLE IF NOT EXISTS ...")
    
    
    client = bigquery.Client()
    conn = Connection(client)
    with Pep249Cursor(conn) as c:
            create_tbl_if_not_exists(c)
    
  19. tswast commented on Feb 10, 2020

    @tswast
    tswast
    on Feb 10, 2020
    Contributor
    More actions

    The DB-API is a bit of an oddball in regards to context managers. I would expect a Curor context manager to commit a transaction but not close the connection, which doesn't apply to BigQuery as it is not a transactional database.

    Reacted by b-long
  20. meredithslota commented on Feb 9, 2022

    @meredithslota
    meredithslota
    on Feb 9, 2022
    Contributor
    More actions

    This feels still like a pressing issue, unless some of the linked issues have solved it (I can't immediately tell). @parthea can you also take a look? Maybe it needs to be moved to https://github.com/googleapis/gapic-generator-python?

  21. meredithslota
    assigned
    parthea
    on Feb 9, 2022
  22. parthea commented on Oct 21, 2023

    @parthea
    parthea
    on Oct 21, 2023
    Contributor
    More actions

    Closing as obsolete. This was completed in googleapis/gapic-generator-python#987

  23. parthea
    closed this as completedon Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @busunkim96
    busunkim96
  • @parthea
    parthea

Labels

api: bigquerydatatransferIssues related to the BigQuery Data Transfer Service API.api: coretype: 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.