Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions google/cloud/sql/connector/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from google.cloud.sql.connector.enums import DriverMapping
from google.cloud.sql.connector.enums import IPTypes
from google.cloud.sql.connector.enums import RefreshStrategy
from google.cloud.sql.connector.exceptions import ConnectorLoopError
from google.cloud.sql.connector.instance import RefreshAheadCache
from google.cloud.sql.connector.lazy import LazyRefreshCache
import google.cloud.sql.connector.pg8000 as pg8000
Expand Down Expand Up @@ -208,27 +207,16 @@ def connect(

Returns:
A DB-API connection to the specified Cloud SQL instance.

Raises:
ConnectorLoopError: Event loop for background refresh is running in
current thread. Error instead of hanging indefinitely.
"""
try:
# check if event loop is running in current thread
if self._loop == asyncio.get_running_loop():
raise ConnectorLoopError(
"Connector event loop is running in current thread!"
"Event loop must be attached to a different thread to prevent blocking code!"
)
# asyncio.get_running_loop will throw RunTimeError if no running loop is present
except RuntimeError:
pass

# if event loop is not in current thread, proceed with connection
connect_task = asyncio.run_coroutine_threadsafe(
self.connect_async(instance_connection_string, driver, **kwargs), self._loop
# connect runs sync database connections on background thread.
# Async database connections should call 'connect_async' directly to
# avoid hanging indefinitely.
connect_future = asyncio.run_coroutine_threadsafe(
self.connect_async(instance_connection_string, driver, **kwargs),
self._loop,
)
return connect_task.result()
return connect_future.result()

async def connect_async(
self, instance_connection_string: str, driver: str, **kwargs: Any
Expand Down
16 changes: 0 additions & 16 deletions tests/unit/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from google.cloud.sql.connector import IPTypes
from google.cloud.sql.connector.client import CloudSQLClient
from google.cloud.sql.connector.exceptions import CloudSQLIPTypeError
from google.cloud.sql.connector.exceptions import ConnectorLoopError
from google.cloud.sql.connector.exceptions import IncompatibleDriverError
from google.cloud.sql.connector.instance import RefreshAheadCache

Expand Down Expand Up @@ -88,21 +87,6 @@ def test_connect_with_unsupported_driver(fake_credentials: Credentials) -> None:
assert exc_info.value.args[0] == "Driver 'bad_driver' is not supported."


@pytest.mark.asyncio
async def test_connect_ConnectorLoopError(fake_credentials: Credentials) -> None:
"""Test that ConnectorLoopError is thrown when Connector.connect
is called with event loop running in current thread."""
current_loop = asyncio.get_running_loop()
connector = Connector(credentials=fake_credentials, loop=current_loop)
# try to connect using current thread's loop, should raise error
pytest.raises(
ConnectorLoopError,
connector.connect,
"my-project:my-region:my-instance",
"pg8000",
)


def test_Connector_Init(fake_credentials: Credentials) -> None:
"""Test that Connector __init__ sets default properties properly."""
with patch("google.auth.default") as mock_auth:
Expand Down
Loading