# Race condition when running background task

**URL:** <https://forums.ankiweb.net/t/race-condition-when-running-background-task/19892>\
**Category:** Development\
**Created:** [May 11, 2022, 4:31am UTC](https://forums.ankiweb.net/t/race-condition-when-running-background-task/19892 "2022-05-11T04:31:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkosov](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rkosov](https://forums.ankiweb.net/u/rkosov)\
**Post date:** [May 11, 2022, 4:31am UTC](https://forums.ankiweb.net/t/race-condition-when-running-background-task/19892/1 "2022-05-11T04:31:10Z")

</div>

I’ve run into a race condition that causes a hang when running a background task and a progress bar. The sequence is

1. Start Function
2. Function has nothing to do or finishes too quickly
3. Success function is called and showInfo displays a message
4. Progress Bar is activated.
5. Because Progress Bar is activated, showInfo cannot be closed
6. Progress Bar hangs because there is nothing to actually do. The Function has already ended.

Code:

```auto
    op = QueryOp(
        parent=mw,
        op=lambda col: batch_get_audio(col),
        success=on_success,
    )

    # Show a progress window
    op.with_progress(label="Fetching Audio").run_in_background()

```

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [May 12, 2022, 1:21am UTC](https://forums.ankiweb.net/t/race-condition-when-running-background-task/19892/2 "2022-05-12T01:21:45Z")

</div>

There’s a reasonable chance the issue is in the context you have not provided. Please provide a minimum full example that we can use to try to reproduce the problem.

---

<div class="post-metadata">

**Author:** ![rkosov](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rkosov](https://forums.ankiweb.net/u/rkosov)\
**Post date:** [May 12, 2022, 3:32am UTC](https://forums.ankiweb.net/t/race-condition-when-running-background-task/19892/3 "2022-05-12T03:32:36Z")

</div>

This is the shortest code that I could think of. It’s really stupid, but it accurately replicates the problem

```auto
def batch_get_audio2(col: Collection):
    return [0, 0]

def on_success(count: tuple) -> None:
    showInfo(f"{count[0]} Meanings Added.\n {count[1]} not found.\n)

 op = QueryOp(
        parent=mw,
        op=lambda col: batch_get_audio2(col),
        success=on_success,
    )

    # Show a progress window
    op.with_progress(label="Fetching Audio").run_in_background()

```

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [May 12, 2022, 3:53am UTC](https://forums.ankiweb.net/t/race-condition-when-running-background-task/19892/4 "2022-05-12T03:53:24Z")

</div>

Could you please provide a full example that is valid Python and that I can drop directly into the add-on folder?

---

<div class="post-metadata">

**Author:** ![rkosov](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rkosov](https://forums.ankiweb.net/u/rkosov)\
**Post date:** [May 12, 2022, 12:06pm UTC](https://forums.ankiweb.net/t/race-condition-when-running-background-task/19892/5 "2022-05-12T12:06:09Z")

</div>

@dae Here is a full example that I tested in Anki to reproduce the error.

```auto
from aqt import mw
from aqt.utils import showInfo
from anki.collection import Collection
from aqt.operations import QueryOp
from aqt.qt import *

def batch_get_audio2(col: Collection):
    return [0, 0]

def on_success(count: tuple) -> None:
    showInfo(f"{count[0]} Meanings Added.\n {count[1]} not found.\n")

def batch_download() -> None:

    op = QueryOp(
            parent=mw,
            op=lambda col: batch_get_audio2(col),
            success=on_success,
        )

    # Show a progress window
    op.with_progress(label="Fetching Audio").run_in_background()

# Add the Item to the Menu
action = QAction("Race Tester", mw)
# set it to call testFunction when it's clicked
qconnect(action.triggered, batch_download)
# and add it to the tools menu

mw.form.menuTools.addAction(action)

```

---

<div class="post-metadata">

**Author:** ![BlueGreenMagick](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@BlueGreenMagick](https://forums.ankiweb.net/u/BlueGreenMagick)\
**Post date:** [May 12, 2022, 4:56pm UTC](https://forums.ankiweb.net/t/race-condition-when-running-background-task/19892/6 "2022-05-12T16:56:55Z")

</div>

I can confirm the error happens in 2.1.50, but it seems to be fixed in latest master

---

<div class="post-metadata">

**Author:** ![Rumo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/rumo/32/1505_2.png) [@Rumo](https://forums.ankiweb.net/u/Rumo)\
**Post date:** [May 12, 2022, 7:39pm UTC](https://forums.ankiweb.net/t/race-condition-when-running-background-task/19892/7 "2022-05-12T19:39:25Z")

</div>

Probably with this, which finishes the progress before calling `success`:

> <https://github.com/ankitects/anki/commit/aea7eb667e2c78de3a99436e458f413b9e765226>
>
> \* Fix progress of chained operations
> 
> Especially aborting colpkg import.
> 
> \* …Notify about missing progress dialog

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [May 13, 2022, 2:58am UTC](https://forums.ankiweb.net/t/race-condition-when-running-background-task/19892/8 "2022-05-13T02:58:41Z")

</div>

This fix is in 2.1.52, which was released today.

---

<div class="post-metadata">

**Author:** ![rkosov](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rkosov](https://forums.ankiweb.net/u/rkosov)\
**Post date:** [May 13, 2022, 12:50pm UTC](https://forums.ankiweb.net/t/race-condition-when-running-background-task/19892/9 "2022-05-13T12:50:10Z")

</div>

I can confirm that this is fixed in 2.1.52. Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [May 1, 2023, 3:15am UTC](https://forums.ankiweb.net/t/race-condition-when-running-background-task/19892/10 "2023-05-01T03:15:40Z")

</div>


