Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Using break in iterate() loop hangs on acquiring lock #294

@gertvdijk

Description

@gertvdijk

I'm using 0.4.1 with SQLite as backend on Python 3.8.5 (Ubuntu 20.04) and facing the following issue using iterate().

My use case is to go over a potential huge number of rows (so I'd like to avoid fetch methods), and break the loop on some condition, e.g. iterating from newest to oldest, stopping when I encounter it's going back in time too far. Simplified:

date_to_stop = datetime(2010, 1, 1)
query = notes_table.select().where(notes_table.c.user == user_id)
async for row in db.iterate(query.order_by(notes_table.c.date.desc())):
    if row[notes_table.c.date] < date_to_stop:
        break
    yield myfunc_row_to_note_object(row)

This break statement does not seem to break the loop, but instead hangs forever. Using Ctrl+C in my command line application shows:

Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 1388, in _shutdown
    lock.acquire()
KeyboardInterrupt: 

This also happens when nesting a for loop, with the use of break in the outer loop and iterate() in the inner, e.g.:

async for note in notes_from_db_generator(db):  # `async for row in db.iterate` inside this function
    if note.date < date_to_stop:
        break

It may be a duplicate of #176, but I'm not sure, I may be using the library in a wrong way? Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions