diff --git a/google/cloud/sql/connector/connector.py b/google/cloud/sql/connector/connector.py index 20235362d..5bb0bfc93 100755 --- a/google/cloud/sql/connector/connector.py +++ b/google/cloud/sql/connector/connector.py @@ -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 @@ -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 diff --git a/tests/unit/test_connector.py b/tests/unit/test_connector.py index 7a59b448e..da4630675 100644 --- a/tests/unit/test_connector.py +++ b/tests/unit/test_connector.py @@ -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 @@ -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: