GoogleCloudPlatform / GoogleCloudPlatform/cloud-sql-python-connector

Update docs to showcase cleanup of `Connector` object

未關閉
#914 15 則留言 0 個 reaction 已指派 1 人 已指派給 @kgala2 在 GitHub 檢視
priority: p2 type: cleanup type: docs
主要語言
Python
星號
344
分支
89
平均合併
5 小時 33 分鐘
30 天內合併 PR
2

描述

### 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_

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。