BlockingError(func_name) when using engine = PGEngine.from_connection_string(url=connection_string) #77

Closed
opened 2026-02-16 05:16:28 -05:00 by yindo · 2 comments
Owner

Originally created by @hypntzed78 on GitHub (Jun 24, 2025).

we dont have from_connection_string() for async method?

my code:

async def make_pg_retriever(
    configuration: BaseConfiguration, embedding_model: Embeddings
) -> AsyncIterator[BaseRetriever]:
    # Establish an asynchronous connection pool to PostgreSQL
    # You'll need to configure your PostgreSQL connection string
    # For example, using environment variables
    connection_string = os.environ.get(
        "PGVECTOR_CONNECTION_STRING",
    )
    # both engine have same error
    engine = await asyncio.to_thread(PGEngine.from_connection_string,connection_string)
    # engine = PGEngine.from_connection_string(
    #     url=connection_string
    # )
    vectorstore = await PGVectorStore.create(

error:

` File "\Downloads\Project\chat-langchain.venv\Lib\site-packages\langchain_postgres\v2\engine.py", line 104, in from_connection_string
cls._default_loop = asyncio.new_event_loop()
^^^^^^^^^^^^^^^^^^^^^^^^
File "\AppData\Local\Programs\Python\Python311\Lib\asyncio\events.py", line 810, in new_event_loop
return get_event_loop_policy().new_event_loop()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "\AppData\Local\Programs\Python\Python311\Lib\asyncio\events.py", line 699, in new_event_loop
return self._loop_factory()
^^^^^^^^^^^^^^^^^^^^
File "\AppData\Local\Programs\Python\Python311\Lib\asyncio\selector_events.py", line 56, in init
self._make_self_pipe()
File "\AppData\Local\Programs\Python\Python311\Lib\asyncio\selector_events.py", line 107, in _make_self_pipe
self._ssock, self._csock = socket.socketpair()
^^^^^^^^^^^^^^^^^^^
File "\AppData\Local\Programs\Python\Python311\Lib\socket.py", line 645, in socketpair
ssock, _ = lsock.accept()
^^^^^^^^^^^^^^
File "Downloads\Project\chat-langchain.venv\Lib\site-packages\blockbuster\blockbuster.py", line 109, in wrapper
raise BlockingError(func_name)

raise BlockingError(func_name)

blockbuster.blockbuster.BlockingError: Blocking call to socket.socket.accept

Heads up! LangGraph dev identified a synchronous blocking call in your code. When running in an ASGI web server, blocking calls can degrade performance for everyone since they tie up the event loop.

Here are your options to fix this:

  1. Best approach: Convert any blocking code to use async/await patterns
    For example, use 'await aiohttp.get()' instead of 'requests.get()'

  2. Quick fix: Move blocking operations to a separate thread
    Example: 'await asyncio.to_thread(your_blocking_function)'

  3. Override (if you can't change the code):

    • For development: Run 'langgraph dev --allow-blocking'
    • For deployment: Set 'BG_JOB_ISOLATED_LOOPS=true' environment variable
      `
Originally created by @hypntzed78 on GitHub (Jun 24, 2025). we dont have from_connection_string() for async method? my code: ``` async def make_pg_retriever( configuration: BaseConfiguration, embedding_model: Embeddings ) -> AsyncIterator[BaseRetriever]: # Establish an asynchronous connection pool to PostgreSQL # You'll need to configure your PostgreSQL connection string # For example, using environment variables connection_string = os.environ.get( "PGVECTOR_CONNECTION_STRING", ) # both engine have same error engine = await asyncio.to_thread(PGEngine.from_connection_string,connection_string) # engine = PGEngine.from_connection_string( # url=connection_string # ) vectorstore = await PGVectorStore.create( ``` error: ` File "\Downloads\Project\chat-langchain\.venv\Lib\site-packages\langchain_postgres\v2\engine.py", line 104, in from_connection_string cls._default_loop = asyncio.new_event_loop() ^^^^^^^^^^^^^^^^^^^^^^^^ File "\AppData\Local\Programs\Python\Python311\Lib\asyncio\events.py", line 810, in new_event_loop return get_event_loop_policy().new_event_loop() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "\AppData\Local\Programs\Python\Python311\Lib\asyncio\events.py", line 699, in new_event_loop return self._loop_factory() ^^^^^^^^^^^^^^^^^^^^ File "\AppData\Local\Programs\Python\Python311\Lib\asyncio\selector_events.py", line 56, in __init__ self._make_self_pipe() File "\AppData\Local\Programs\Python\Python311\Lib\asyncio\selector_events.py", line 107, in _make_self_pipe self._ssock, self._csock = socket.socketpair() ^^^^^^^^^^^^^^^^^^^ File "\AppData\Local\Programs\Python\Python311\Lib\socket.py", line 645, in socketpair ssock, _ = lsock.accept() ^^^^^^^^^^^^^^ File "Downloads\Project\chat-langchain\.venv\Lib\site-packages\blockbuster\blockbuster.py", line 109, in wrapper raise BlockingError(func_name) raise BlockingError(func_name) blockbuster.blockbuster.BlockingError: Blocking call to socket.socket.accept Heads up! LangGraph dev identified a synchronous blocking call in your code. When running in an ASGI web server, blocking calls can degrade performance for everyone since they tie up the event loop. Here are your options to fix this: 1. Best approach: Convert any blocking code to use async/await patterns For example, use 'await aiohttp.get()' instead of 'requests.get()' 2. Quick fix: Move blocking operations to a separate thread Example: 'await asyncio.to_thread(your_blocking_function)' 3. Override (if you can't change the code): - For development: Run 'langgraph dev --allow-blocking' - For deployment: Set 'BG_JOB_ISOLATED_LOOPS=true' environment variable `
yindo closed this issue 2026-02-16 05:16:28 -05:00
Author
Owner

@hypntzed78 commented on GitHub (Jun 24, 2025):

I have try with create() and create_sync(), both still block bluster

@contextmanager
def make_pg_retriever(
    configuration: BaseConfiguration, embedding_model: Embeddings
) -> Iterator[BaseRetriever]:
    connection_string = os.environ.get("PGVECTOR_CONNECTION_STRING",)
    engine = PGEngine.from_connection_string(url=connection_string)

    try:
        vectorstore = PGVectorStore.create_sync(
        engine=engine,
        embedding_service=embedding_model,
        table_name=WEAVIATE_DOCS_INDEX_NAME,  # The table must already exist!
        metadata_columns=["source", "title", "description", "language"]
        # ... other optional parameters
    )
        search_kwargs = {**configuration.search_kwargs, "return_uuids": True}
        retriever = vectorstore.as_retriever(search_kwargs=search_kwargs)
        yield retriever

    finally:
        engine.close()

and

@asynccontextmanager  # Use asynccontextmanager for async operations
async def make_pg_retriever(
    configuration: BaseConfiguration, embedding_model: Embeddings
) -> AsyncIterator[BaseRetriever]:
    # Establish an asynchronous connection pool to PostgreSQL
    # You'll need to configure your PostgreSQL connection string
    # For example, using environment variables
    connection_string = os.environ.get(
        "PGVECTOR_CONNECTION_STRING",
    )
    # engine = await asyncio.to_thread(PGEngine.from_connection_string,url= connection_string)
    engine = await asyncio.to_thread(lambda: PGEngine.from_connection_string(url=connection_string))

    # engine = PGEngine.from_connection_string(
    #     url=connection_string
    # )
    vectorstore = await AsyncPGVectorStore.create(  #use PGVectorStore is the same
        engine=engine,
        embedding_service=embedding_model, #why dont use like ingest.py?
        table_name=WEAVIATE_DOCS_INDEX_NAME,  # The table must already exist!
        metadata_columns=["source", "title", "description", "language"]
        # ... other optional parameters
    )

    search_kwargs = {**configuration.search_kwargs}
        # AsyncPGVectorStore's as_retriever might not directly support 'return_uuids'
        # The returned documents will likely have the primary key or a unique identifier
        # as part of their metadata, which you can access.
    yield vectorstore.as_retriever(search_kwargs=search_kwargs)
@hypntzed78 commented on GitHub (Jun 24, 2025): I have try with create() and create_sync(), both still block bluster ``` @contextmanager def make_pg_retriever( configuration: BaseConfiguration, embedding_model: Embeddings ) -> Iterator[BaseRetriever]: connection_string = os.environ.get("PGVECTOR_CONNECTION_STRING",) engine = PGEngine.from_connection_string(url=connection_string) try: vectorstore = PGVectorStore.create_sync( engine=engine, embedding_service=embedding_model, table_name=WEAVIATE_DOCS_INDEX_NAME, # The table must already exist! metadata_columns=["source", "title", "description", "language"] # ... other optional parameters ) search_kwargs = {**configuration.search_kwargs, "return_uuids": True} retriever = vectorstore.as_retriever(search_kwargs=search_kwargs) yield retriever finally: engine.close() ``` and ``` @asynccontextmanager # Use asynccontextmanager for async operations async def make_pg_retriever( configuration: BaseConfiguration, embedding_model: Embeddings ) -> AsyncIterator[BaseRetriever]: # Establish an asynchronous connection pool to PostgreSQL # You'll need to configure your PostgreSQL connection string # For example, using environment variables connection_string = os.environ.get( "PGVECTOR_CONNECTION_STRING", ) # engine = await asyncio.to_thread(PGEngine.from_connection_string,url= connection_string) engine = await asyncio.to_thread(lambda: PGEngine.from_connection_string(url=connection_string)) # engine = PGEngine.from_connection_string( # url=connection_string # ) vectorstore = await AsyncPGVectorStore.create( #use PGVectorStore is the same engine=engine, embedding_service=embedding_model, #why dont use like ingest.py? table_name=WEAVIATE_DOCS_INDEX_NAME, # The table must already exist! metadata_columns=["source", "title", "description", "language"] # ... other optional parameters ) search_kwargs = {**configuration.search_kwargs} # AsyncPGVectorStore's as_retriever might not directly support 'return_uuids' # The returned documents will likely have the primary key or a unique identifier # as part of their metadata, which you can access. yield vectorstore.as_retriever(search_kwargs=search_kwargs) ```
Author
Owner

@averikitsch commented on GitHub (Jul 2, 2025):

Hi @ladylazy9x , from_connection_string is not a blocking calling. You shouldn't need to use the asyncio.to_thread. That is handled for you in the implementation. I can not reproduce the error. Your code is running as expected.

@averikitsch commented on GitHub (Jul 2, 2025): Hi @ladylazy9x , `from_connection_string` is not a blocking calling. You shouldn't need to use the asyncio.to_thread. That is handled for you in the implementation. I can not reproduce the error. Your code is running as expected.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langchain-postgres#77