Skip to content

No exception thrown and no timeout when using a Connector that is closed #1268

Closed

Description

@seeker25

Bug Description

No exception thrown and no timeout when using a Connector that is closed, code stays stuck waiting

Example code (or command)

import os
from google.cloud.sql.connector import Connector
from sqlalchemy import create_engine

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'service-account.json'
connector = Connector()

def get_iam_connection():
	"""Establish connection using IAM authentication."""
	return connector.connect(
		"unix-socket",
		"pg8000",
		ip_type="public",
		db="dbname",
		user="dbuser",
		enable_iam_auth=True,
	)

engine = create_engine(
	"postgresql+pg8000://",
	creator=lambda: get_iam_connection(),
	pool_size=5,
	max_overflow=2,
	pool_timeout=10,
	pool_recycle=1800,
	connect_args={"use_native_uuid": False},
)

# Intentionally close the connector before it's used
connector.close()

# It gets stuck waiting on the line below
with engine.connect() as conn:
        # The line below this one is never called, it's stuck on the line above.
	result = conn.execute(text("SELECT version()")).fetchone()
	print(f"Connection successful. Database version: {result[0]}")

Steps to reproduce?

  1. Close the connection intentionally (I ran into this accidentally) before trying to connect()
  2. Notice the code stuck forever with no timeout and no exception thrown

Environment

  1. OS type and version: Ubuntu 20.04.4 LTS
  2. Python version: 3.12.2
  3. Cloud SQL Python Connector version: 1.13.0 to 1.18.0

Additional Details

I think it's reasonable to expect the connector would throw an exception or eventually timeout?

I've also tried setting timeout in the connector, tried setting timeout as part of the connect_args under create_engine same result.

Activity

  1. added
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    on Apr 7, 2025
  2. changed the title [-]No exception thrown only a freeze when using a connection that is closed[/-] [+]No exception thrown only code stuck forever when using a connection that is closed[/+] on Apr 7, 2025
  3. changed the title [-]No exception thrown only code stuck forever when using a connection that is closed[/-] [+]No exception thrown and no timeout when using a connection that is closed[/+] on Apr 7, 2025
  4. changed the title [-]No exception thrown and no timeout when using a connection that is closed[/-] [+]No exception thrown and no timeout when using a Connector that is closed[/+] on Apr 7, 2025
  5. added
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Apr 7, 2025
  6. jackwotherspoon commented on Apr 7, 2025

    @jackwotherspoon
    Collaborator

    Thanks for raising an issue @seeker25 😄

    I totally agree, we should definitely add a check to the top of our connector.connect_async code to see if the connector has been closed and if it has throw an error 👍

    @kgala2 Do you mind picking this up in the coming sprint?

  7. kgala2 commented on Apr 7, 2025

    @kgala2
    Collaborator

    Thanks for raising an issue @seeker25 😄

    I totally agree, we should definitely add a check to the top of our connector.connect_async code to see if the connector has been closed and if it has throw an error 👍

    cloud-sql-python-connector/google/cloud/sql/connector/connector.py

    Line 252 in e7f1068

    async def connect_async(
    @kgala2 Do you mind picking this up in the coming sprint?

    Thanks for the check suggestion. I'll add this task to my sprint.

  8. kgala2 commented on Oct 28, 2025

    @kgala2
    Collaborator

    PR - #1269 is work in progress

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

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.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions