Skip to content

Bug: Can't use async concurrency with websocket connection. #164

@Anton-2

Description

@Anton-2

Describe the bug

When using AsyncSurreal with web socket (ws:// url), any attempt to have concurrent async requests fails with "cannot call recv while another coroutine is already running recv or recv_streaming".

This does not happen with the http client in v1.0.3.

Probably related to #101 and maybe #160 (async issues)

What is the status of this ? Should we use the http connection for async or is it a bug ? I remember the http connection having restrictions, and ws being the recommended way to use SurrealDB, so it seems strange to me.

#101 is closed with a message saying that concurrency issues are solved #101 (comment) but how can we "get 100% control over the async coroutine" ?

I really want to use SurrealDB, but for this async support in python should be rock solid.

Steps to reproduce

Here is a demo program (adapt for your auth, namespace and database)
Change the connection method (http to ws in AsyncSurreal("http://localhost:8000/rpc")) to go from working to failure.

import asyncio
from surrealdb import AsyncSurreal

async def main(n):
    async with AsyncSurreal("http://localhost:8000/rpc") as db:
        await db.signin({"username": 'root', "password": 'root'})
        await db.use("test", "test")
        async with asyncio.TaskGroup() as tg:
            tasks = [tg.create_task(db.query("select $p**2 as ret from {}", dict(p=num))) for num in range(n)]
    return [t.result()[0]["ret"] for t in tasks]

if __name__ == "__main__":
    print(asyncio.run(main(5)))

Expected behaviour

Working : (with http:) [0, 1, 4, 9, 16]
Not Working : (with ws:) websockets.exceptions.ConcurrencyError: cannot call recv while another coroutine is already running recv or recv_streaming

SurrealDB version

surreal version 2.2.1 for macos on aarch64

surrealdb.py version

surrealdb.py 1.0.3 for macOS on aarch64 using python 3.13.1

Contact Details

No response

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions