GoogleCloudPlatform / GoogleCloudPlatform/cloud-sql-python-connector

Update docs to showcase cleanup of `Connector` object

Abierto
#914 15 comentarios 0 reacciones 1 asignado Asignado a @kgala2 Ver en GitHub
priority: p2 type: cleanup type: docs
Lenguaje dominante
Python
Estrellas
344
Forks
89
Merge medio
5 h 33 min
PR fusionados (30 d)
2

Descripción

### 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: ._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: https://github.com/GoogleCloudPlatform/cloud-sql-python-connector/blob/406b383728b9aabda77d6fd4326ff7ce8d955557/google/cloud/sql/connector/instance.py#L376

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_

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.