GoogleCloudPlatform / GoogleCloudPlatform/cloud-sql-python-connector

Update docs to showcase cleanup of `Connector` object

オープン
#914 コメント 15 件 リアクション 0 件 担当者 1 名 @kgala2 に割り当て済み GitHub で見る
priority: p2 type: cleanup type: docs
主要言語
Python
スター
344
フォーク
89
平均マージ
5時間 33分
マージ済み PR(30日)
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 を短くまとめたダイジェスト。