Skip to content

Async connection close method doesn't terminate the ongoing transaction #608

@khushboovashi

Description

@khushboovashi

If the async query is running in the thread and the connection is explicitly closed, the ongoing execution doesn't get terminated (we can still see the process in the terminal). Also, the execute function ( await _c.execute("SELECT pg_sleep(20);")) runs indefinitely.

Here is the test code:

async def test_connect():
    conn = await AsyncConnection.connect(
        host='localhost',
        port=5432,
        dbname='db1',
        user='postgres',
        password='xxxxxx'
    )

    return conn

conn = asyncio.run(test_connect())
_cur = _cursor(conn)

def asyn_exec_query(c):
    try:
        async def _execute(_c):
            print("Execution Start.")
            await _c.execute("SELECT pg_sleep(20);")
            print("Execution Complete.")
        asyncio.run(_execute(c))
    except Exception as e:
        print(str(e))


async def main():
    _thread = threading.Thread(target=asyn_exec_query, args=(_cur,))
    _thread.start()
    print("Before Sleep")
    time.sleep(3)
    print("After sleep")
    await conn.close()
    print("Finish")

asyncio.run(main())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions