# \[Question about add-on development\] How to use popup to display the details of exceptions in background task?

**URL:** <https://forums.ankiweb.net/t/question-about-add-on-development-how-to-use-popup-to-display-the-details-of-exceptions-in-background-task/31678>\
**Category:** Development\
**Created:** [June 30, 2023, 3:58am UTC](https://forums.ankiweb.net/t/question-about-add-on-development-how-to-use-popup-to-display-the-details-of-exceptions-in-background-task/31678 "2023-06-30T03:58:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![L.M.Sherlock](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/l.m.sherlock/32/16770_2.png) [@L.M.Sherlock](https://forums.ankiweb.net/u/L.M.Sherlock)\
**Post date:** [June 30, 2023, 3:58am UTC](https://forums.ankiweb.net/t/question-about-add-on-development-how-to-use-popup-to-display-the-details-of-exceptions-in-background-task/31678/1 "2023-06-30T03:58:31Z")

</div>

When the python code raises an exception in the main thread, a popup will show the details about the exception:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/3/3d1723636bd6c9f26334b196754d117c83d0803b.png)

But when I move the task into background, the error will be silent. The progress bar gets stuck:

![image](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/7/7b34e65d8805e35ff03a0ca9330005a41f82fafe.png)

Are there any methods to use popup to display the details of exceptions in background task?

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [June 30, 2023, 12:23pm UTC](https://forums.ankiweb.net/t/question-about-add-on-development-how-to-use-popup-to-display-the-details-of-exceptions-in-background-task/31678/2 "2023-06-30T12:23:22Z")

</div>

It appears you’re trying to call GUI code inside the background task (.setChecked). This can result in Anki hanging/crashing.

The currently recommended approach is to use CollectionOp/QueryOp to run background tasks. See [Background Operations - Writing Anki Add-ons](https://addon-docs.ankiweb.net/background-ops.html) for an example. You can add a failure handler and call showWarning/tooltip inside it.

---

<div class="post-metadata">

**Author:** ![L.M.Sherlock](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/l.m.sherlock/32/16770_2.png) [@L.M.Sherlock](https://forums.ankiweb.net/u/L.M.Sherlock)\
**Post date:** [June 30, 2023, 3:43pm UTC](https://forums.ankiweb.net/t/question-about-add-on-development-how-to-use-popup-to-display-the-details-of-exceptions-in-background-task/31678/3 "2023-06-30T15:43:45Z")

</div>

But I didn’t call GUI code. The popup of error is called by Anki itself.

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [June 30, 2023, 5:59pm UTC](https://forums.ankiweb.net/t/question-about-add-on-development-how-to-use-popup-to-display-the-details-of-exceptions-in-background-task/31678/4 "2023-06-30T17:59:48Z")

</div>

I mean the `menu_auto_disperse.setCheked` line in your screenshot (If that’s called in the background). Can you post a code example?

---

<div class="post-metadata">

**Author:** ![L.M.Sherlock](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/l.m.sherlock/32/16770_2.png) [@L.M.Sherlock](https://forums.ankiweb.net/u/L.M.Sherlock)\
**Post date:** [June 30, 2023, 6:11pm UTC](https://forums.ankiweb.net/t/question-about-add-on-development-how-to-use-popup-to-display-the-details-of-exceptions-in-background-task/31678/5 "2023-06-30T18:11:15Z")

</div>

The Error is just a example of popup. It is not the error happened in my background task.

---

<div class="post-metadata">

**Author:** ![L.M.Sherlock](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/l.m.sherlock/32/16770_2.png) [@L.M.Sherlock](https://forums.ankiweb.net/u/L.M.Sherlock)\
**Post date:** [June 30, 2023, 6:11pm UTC](https://forums.ankiweb.net/t/question-about-add-on-development-how-to-use-popup-to-display-the-details-of-exceptions-in-background-task/31678/6 "2023-06-30T18:11:53Z")

</div>

For example, if I raise ValueException in the background task.

---

<div class="post-metadata">

**Author:** ![L.M.Sherlock](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/l.m.sherlock/32/16770_2.png) [@L.M.Sherlock](https://forums.ankiweb.net/u/L.M.Sherlock)\
**Post date:** [June 30, 2023, 6:14pm UTC](https://forums.ankiweb.net/t/question-about-add-on-development-how-to-use-popup-to-display-the-details-of-exceptions-in-background-task/31678/7 "2023-06-30T18:14:28Z")

</div>

> <https://github.com/open-spaced-repetition/fsrs4anki-helper/blob/8abb058529fff1ca722325cf92b3a5979200fd23/reschedule.py#L277>

What if fsrs.due\_cnt\_perday\_from\_first\_day[due\_before] raise a indexError?

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [June 30, 2023, 7:37pm UTC](https://forums.ankiweb.net/t/question-about-add-on-development-how-to-use-popup-to-display-the-details-of-exceptions-in-background-task/31678/8 "2023-06-30T19:37:11Z")

</div>

My guess is that the progress dialog is getting stuck because mw.progress.finish() is not getting called when an exception occurs. I recommend removing the on\_finish functions and putting the code in the on\_done handler of mw.taskman.run\_in\_background() instead:

```diff
diff --git a/disperse_siblings.py b/disperse_siblings.py
index d6035a4..c52e1a4 100644
--- a/disperse_siblings.py
+++ b/disperse_siblings.py
@@ -76,7 +76,13 @@ def disperse(siblings):
     return best_due_dates
 
 def disperse_siblings(did, filter_flag=False, filtered_nid_string="", text_from_reschedule=""):
- mw.taskman.run_in_background(lambda: disperse_siblings_backgroud(did, filter_flag, filtered_nid_string, text_from_reschedule))  
+ def on_done(future):
+ mw.progress.finish()
+ tooltip(future.result())
+ mw.col.reset()
+ mw.reset()
+
+ mw.taskman.run_in_background(lambda: disperse_siblings_backgroud(did, filter_flag, filtered_nid_string, text_from_reschedule), on_done)
 
 def disperse_siblings_backgroud(did, filter_flag=False, filtered_nid_string="", text_from_reschedule=""):
     custom_scheduler = check_fsrs4anki(mw.col.all_config())
@@ -126,13 +132,6 @@ def disperse_siblings_backgroud(did, filter_flag=False, filtered_nid_string="",

     finished_text = f"{text_from_reschedule +', ' if text_from_reschedule != '' else ''}{card_cnt} cards in {note_cnt} notes dispersed."

- def on_finish():
- tooltip(finished_text)
- mw.progress.finish()
- mw.col.reset()
- mw.reset()
-
- mw.taskman.run_on_main(on_finish)
     return finished_text

 # https://stackoverflow.com/questions/68180974/given-n-points-where-each-point-has-its-own-range-adjust-all-points-to-maximize      
diff --git a/reschedule.py b/reschedule.py
index ae91c9c..b12e908 100644
--- a/reschedule.py
+++ b/reschedule.py
@@ -101,17 +101,21 @@ class FSRS:
 def reschedule(did, recent=False, filter_flag=False, filtered_cids={}, filtered_nid_string=""):

     def on_done(future):
+ mw.progress.finish()
         tooltip(future.result())
+ mw.col.reset()
+ mw.reset()
+

     if filter_flag and len(filtered_cids) > 0:
- fut = mw.taskman.run_in_background(lambda: reschedule_background(did, recent, filter_flag, filtered_cids))
+ fut = mw.taskman.run_in_background(lambda: reschedule_background(did, recent, filter_flag, filtered_cids), on_done)
         config = Config()
         config.load()
         if config.auto_disperse:
             text = fut.result()
             fut = mw.taskman.run_in_background(lambda: disperse_siblings_backgroud(did, filter_flag, filtered_nid_string, text_from_reschedule=text), on_done)
     else:
- fut = mw.taskman.run_in_background(lambda: reschedule_background(did, recent, filter_flag, filtered_cids))
+ fut = mw.taskman.run_in_background(lambda: reschedule_background(did, recent, filter_flag, filtered_cids), on_done)

     return fut

@@ -287,11 +291,4 @@ def reschedule_background(did, recent=False, filter_flag=False, filtered_cids={}

     finished_text = f"{cnt} cards rescheduled"

- def on_finish():
- tooltip(finished_text)
- mw.progress.finish()
- mw.col.reset()
- mw.reset()
-    
- mw.taskman.run_on_main(on_finish)
     return finished_text

```

An important thing to notice is that mw.progress.finish() should be called before future.result() to ensure the progress dialog is proerly closed if an exception occurs.

---

<div class="post-metadata">

**Author:** ![L.M.Sherlock](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/l.m.sherlock/32/16770_2.png) [@L.M.Sherlock](https://forums.ankiweb.net/u/L.M.Sherlock)\
**Post date:** [July 1, 2023, 5:49am UTC](https://forums.ankiweb.net/t/question-about-add-on-development-how-to-use-popup-to-display-the-details-of-exceptions-in-background-task/31678/9 "2023-07-01T05:49:01Z")

</div>

Thank you very much!

---

<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:** [July 31, 2023, 5:49am UTC](https://forums.ankiweb.net/t/question-about-add-on-development-how-to-use-popup-to-display-the-details-of-exceptions-in-background-task/31678/10 "2023-07-31T05:49:39Z")

</div>

Closed on July 31, 2023, 5:49am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
