googleapis / langchain-google-cloud-sql-pg-python

Apache License 2.0
29 stars 11 forks source link

RuntimeError: Timeout context manager should be used inside a task #183

Closed nprime496 closed 2 months ago

nprime496 commented 3 months ago

Thanks for stopping by to let us know something could be better!

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

Please run down the following list and make sure you've tried the usual "quick fixes":

If you are still having issues, please be sure to include as much information as possible:

Environment details

Steps to reproduce

  1. Have a connection error drop occasionally - hard to reproduce consistently

It is exactly the same problem as https://github.com/googleapis/langchain-google-alloydb-pg-python/issues/160 which is solved by https://github.com/googleapis/langchain-google-alloydb-pg-python/pull/162. I am wondering if the solution has been shipped here since I have the same problem.

Code example

        # Create a vector store
        db = PostgresVectorStore.create_sync(  # Use .create() to initialize an async vector store
            engine=engine,
            table_name=table_name,
            embedding_service=embedding,
            metadata_columns=[
                "source_document_page",
                "source_document",
                "source_document_url",
            ],  # IMPORTANT
            content_column="content",
            id_column="document_id",
        )

Stack trace

 "Traceback (most recent call last):
    db = PostgresVectorStore.create_sync(  # Use .create() to initialize an async vector store
  File "/usr/local/lib/python3.10/dist-packages/langchain_google_cloud_sql_pg/vectorstore.py", line 259, in create_sync
    return engine._run_as_sync(coro)
  File "/usr/local/lib/python3.10/dist-packages/langchain_google_cloud_sql_pg/engine.py", line 341, in _run_as_sync
    return asyncio.run_coroutine_threadsafe(coro, self._loop).result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 458, in result
    return self.__get_result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/usr/local/lib/python3.10/dist-packages/langchain_google_cloud_sql_pg/vectorstore.py", line 145, in create
    results = await engine._afetch(stmt)
  File "/usr/local/lib/python3.10/dist-packages/langchain_google_cloud_sql_pg/engine.py", line 322, in _afetch
    async with self._engine.connect() as conn:
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/ext/asyncio/base.py", line 121, in __aenter__
    return await self.start(is_ctxmanager=True)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/ext/asyncio/engine.py", line 273, in start
    await greenlet_spawn(self.sync_engine.connect)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/util/_concurrency_py3k.py", line 201, in greenlet_spawn
    result = context.throw(*sys.exc_info())
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/engine/base.py", line 3278, in connect
    return self._connection_cls(self)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/engine/base.py", line 146, in __init__
    self._dbapi_connection = engine.raw_connection()
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/engine/base.py", line 3302, in raw_connection
    return self.pool.connect()
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/pool/base.py", line 449, in connect
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/pool/base.py", line 1263, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/pool/base.py", line 712, in checkout
    rec = pool._do_get()
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/pool/impl.py", line 179, in _do_get
    with util.safe_reraise():
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/util/langhelpers.py", line 146, in __exit__
    raise exc_value.with_traceback(exc_tb)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/pool/impl.py", line 177, in _do_get
    return self._create_connection()
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/pool/base.py", line 390, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/pool/base.py", line 674, in __init__
    self.__connect()
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/pool/base.py", line 900, in __connect
    with util.safe_reraise():
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/util/langhelpers.py", line 146, in __exit__
    raise exc_value.with_traceback(exc_tb)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/pool/base.py", line 896, in __connect
    self.dbapi_connection = connection = pool._invoke_creator(self)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/pool/base.py", line 362, in <lambda>
    return lambda rec: creator_fn()
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/ext/asyncio/engine.py", line 115, in creator
    return sync_engine.dialect.dbapi.connect(  # type: ignore
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 932, in connect
    await_only(creator_fn(*arg, **kw)),
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/util/_concurrency_py3k.py", line 132, in await_only
    return current.parent.switch(awaitable)  # type: ignore[no-any-return,attr-defined] # noqa: E501
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/util/_concurrency_py3k.py", line 196, in greenlet_spawn
    value = await result
  File "/usr/local/lib/python3.10/dist-packages/langchain_google_cloud_sql_pg/engine.py", line 245, in getconn
    conn = await cls._connector.connect_async(  # type: ignore
  File "/usr/local/lib/python3.10/dist-packages/google/cloud/sql/connector/connector.py", line 341, in connect_async
    conn_info = await cache.connect_info()
  File "/usr/local/lib/python3.10/dist-packages/google/cloud/sql/connector/lazy.py", line 103, in connect_info
    conn_info = await self._client.get_connection_info(
  File "/usr/local/lib/python3.10/dist-packages/google/cloud/sql/connector/client.py", line 271, in get_connection_info
    metadata = await metadata_task
  File "/usr/local/lib/python3.10/dist-packages/google/cloud/sql/connector/client.py", line 128, in _get_metadata
    resp = await self._client.get(url, headers=headers)
  File "/usr/local/lib/python3.10/dist-packages/aiohttp/client.py", line 573, in _request
    with timer:
  File "/usr/local/lib/python3.10/dist-packages/aiohttp/helpers.py", line 693, in __enter__
    raise RuntimeError(
RuntimeError: Timeout context manager should be used inside a task"

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

averikitsch commented 2 months ago

Hi @nprime496, the solution for AlloyDB has also been implemented here. Can you provide more information on how you are initializing the PostgresEngine?

@jackwotherspoon Do you have any recommendations?

nprime496 commented 2 months ago

Hi @averikitsch thanks for your quick response !

It was a problem on my side apparently, I don't fully understand how it ended up with this error but all is working fine now.

Thanks for the feature!