Skip to content

Update docs to showcase cleanup of Connector object #914

@lauraseidler

Description

@lauraseidler

Bug Description

We use the connector with IAM Auth + Cloud SQL for Postgres. It generally works okay, but we are occasionally seeing errors on shut down of our application server that look like this:

ERROR:google.cloud.sql.connector.instance:['XXX']: An error occurred while performing refresh. Scheduling another refresh attempt immediately

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/google/cloud/sql/connector/instance.py", line 376, in _refresh_task
    refresh_data = await refresh_task
  File "/usr/local/lib/python3.10/site-packages/google/cloud/sql/connector/instance.py", line 311, in _perform_refresh
    metadata = await metadata_task
  File "/usr/local/lib/python3.10/site-packages/google/cloud/sql/connector/refresh_utils.py", line 106, in _get_metadata
    resp = await client_session.get(url, headers=headers, raise_for_status=True)
  File "/usr/local/lib/python3.10/site-packages/aiohttp/client.py", line 586, in _request
    await resp.start(conn)
  File "/usr/local/lib/python3.10/site-packages/aiohttp/client_reqrep.py", line 905, in start
    message, payload = await protocol.read()  # type: ignore[union-attr]
  File "/usr/local/lib/python3.10/site-packages/aiohttp/streams.py", line 616, in read
    await self._waiter
aiohttp.client_exceptions.ClientOSError: [Errno 32] Broken pipe

ERROR:asyncio:Task exception was never retrieved

future: <Task finished name='Task-507' coro=<Instance._schedule_refresh.<locals>._refresh_task() done, defined at /usr/local/lib/python3.10/site-packages/google/cloud/sql/connector/instance.py:365> exception=ClientOSError(32, 'Broken pipe')>

The exception itself may vary - mostly aiohttp.client_exceptions.ClientOSError: [Errno 32] Broken pipe, but we've also seen aiohttp.client_exceptions.ServerDisconnectedError: Server disconnected.

To me, this looks like an async task is not checked for exceptions, even though at least the one in the referenced line looks okay:

refresh_data = await refresh_task

Since this means these errors are only logged when the application shuts down, this makes it somewhat hard to debug what's causing these connection issues, and if they are causing actual issues or the connection is re-established successfully.

Example code (or command)

No response

Stacktrace

No response

Steps to reproduce?

  1. Use Python connector as documented here https://cloud.google.com/sql/docs/postgres/iam-logins#log-in-with-automatic
  2. Run SQL queries for a while (we're doing it within Cloud Run, and it's only reproducible under load, not locally)
  3. Shut down application

Environment

  1. OS type and version: Debian 11.7 (python:3.10.11-slim docker image)
  2. Python version: 3.10.11
  3. Cloud SQL Python Connector version: 1.4.3

Additional Details

No response

Metadata

Metadata

Assignees

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: cleanupAn internal cleanup or hygiene concern.type: docsImprovement to the documentation for an API.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions