albertlauncher / albertlauncher/albert-plugin-python
Coroutine getting lost after sleep
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 3
- Forks
- 3
- PR merge metrics
- No merged PRs in 30d
Description
I'm working on a plugin with a GeneratorQueryHandler which yields one item as a result, and then sleeps for a bit, and then fetches a result from an API and yields those items as well. So basically, it yields two chunks:
- "Web search"
- First 5 results of the suggestions
But I've noticed that the coroutine seems to just stop getting called sometimes. I was able to boil it down to a minimal reproducer:
import time
from albert import *
md_iid = "5.0"
md_name = "chunktest"
md_description = "testing multiple chunks of items"
md_authors = ["@brenns10"]
class Plugin(PluginInstance, GeneratorQueryHandler):
def __init__(self):
PluginInstance.__init__(self)
GeneratorQueryHandler.__init__(self)
def defaultTrigger(self):
return "chunktest "
def items(self, ctx):
for i in range(5):
if not ctx.isValid:
info("exiting early")
return
info(f"yielding item {i}")
yield [
StandardItem(
id=f"item{i}",
text=f"Item {i}",
subtext="details",
actions=[
Action(
id="act",
text="Open Google",
callable=lambda: runTerminal("echo hi"),
)
],
)
]
time.sleep(0.1)
info("finished sleeping")
info("done")
When I load this plugin, and then type "chunktest asdf" (type really fast), I only get "Item 0", and the logs look like this:
17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:52 [warn:albert.wbm] Item retured null icon: python.chunktest.item0
17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:52 [info:albert.python.chunktest] finished sleeping
17:25:52 [info:albert.python.chunktest] yielding item 1
17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:53 [info:albert.python.chunktest] yielding item 0
17:25:53 [info:albert.python.chunktest] yielding item 0
The remaining 4 items never get populated. Here is a video that shows what I mean.
However, if you go ahead and move the time.sleep() call just before the yield statement, rather than after it, you'll get the behavior I expect. That is, the results keep getting replaced each time I type a new keystroke, and they slowly populate whenever I pause. Here's the video of this behavior.
It seems to me like there's something going on with this time.sleep() call directly after the yield. I notice I never see the "exiting early" message logged, which may or may not be related.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal reproducer's GeneratorQueryHandler.items method and observe its behavior while typing "chunktest asdf" quickly. Compare sleeping after the yield with sleeping before it, and trace how coroutine cancellation and ctx.isValid are handled. Done means all five item chunks continue to populate after a post-yield sleep without losing the coroutine.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 50/100