Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shutdown trigger with task.unique() fails #117

Closed
dlashua opened this issue Dec 13, 2020 · 1 comment
Closed

shutdown trigger with task.unique() fails #117

dlashua opened this issue Dec 13, 2020 · 1 comment

Comments

@dlashua
Copy link
Contributor

dlashua commented Dec 13, 2020

I've finally managed to get this boiled down and reproduceable.

  1. Install this code in a script:
@time_trigger('startup')
def test_startup():
    task.unique('test_counter')
    log.info('starting')
    cnt = 0
    while True:
        cnt += 1
        log.info(f'count {cnt}')
        task.sleep(1)

@time_trigger('shutdown')
def test_shutdown():
    task.unique('test_counter')
    log.info('stopping')
  1. pyscript.reload to make it active. See it counting.
  2. save the file again to update mtime. pyscript.reload. See it counting again.
  3. repeat step 3 one more time. See the error below in logs.
2020-12-13 06:29:01 INFO (MainThread) [custom_components.pyscript.global_ctx] Reloaded /config/pyscript/shutdowntest.py
2020-12-13 06:29:01 INFO (MainThread) [custom_components.pyscript.file.shutdowntest.test_shutdown] stopping
2020-12-13 06:29:01 INFO (MainThread) [custom_components.pyscript.file.shutdowntest.test_startup] starting
2020-12-13 06:29:01 INFO (MainThread) [custom_components.pyscript.file.shutdowntest.test_startup] count 1
2020-12-13 06:29:01 ERROR (MainThread) [custom_components.pyscript.function] task_reaper: got exception Traceback (most recent call last):
  File "/config/custom_components/pyscript/function.py", line 97, in task_reaper
    await cmd[1]
RuntimeError: await wasn't used with future

2020-12-13 06:29:02 INFO (MainThread) [custom_components.pyscript.file.shutdowntest.test_startup] count 2

Once this error is produced, while the counter itself keeps counting, many other pyscript scripts stop triggering reliably as they usually do.

  1. Repeat step 3 again and you will now see there are two counters. The previous one didn't stop, but a new one started.
  2. Do it again and you now have three counters. etc, etc.
  3. To fix it, comment out the script added in step 1 and restart home assistant. (without restarting home assistant and just doing pyscript.reload, only the most recent counter is stopped)
@craigbarratt
Copy link
Member

craigbarratt commented Dec 16, 2020

Thanks for finding a simple test that fails - this fails for me too in the manner you describe. I've been investigating what's going wrong, but I've had limited time the last few days.

There's one bug that I've fixed, but it still isn't working correctly. The shutdown trigger is definitely causing some additional yet-to-be-determined problem, related to the deletion and recreation of the global context on reload using a helper task to execute all the shutdown triggers, rather than creating a new task per shutdown trigger.

@craigbarratt craigbarratt changed the title await wasn't used with future shutdown trigger with task.unique() fails Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants