elad-bar / ha-shinobi

Shinobi Video custom component for HA
53 stars 5 forks source link

Non-thread-safe operation invoked on an event loop other than the current one #54

Closed chemelli74 closed 1 year ago

chemelli74 commented 1 year ago

Hi, trying to debug #53 , I found the following issue:

2023-09-25 08:36:00.685 ERROR (MainThread) [homeassistant] Error doing job: Future exception was never retrieved:   File "/usr/local/lib/python3.11/runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/lib/python3.11/runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "/home/vscode/.vscode-server/extensions/ms-python.python-2023.16.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module>
    cli.main()
  File "/home/vscode/.vscode-server/extensions/ms-python.python-2023.16.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main
    run()
  File "/home/vscode/.vscode-server/extensions/ms-python.python-2023.16.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 317, in run_module
    run_module_as_main(options.target, alter_argv=True)
  File "/home/vscode/.vscode-server/extensions/ms-python.python-2023.16.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 238, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/vscode/.vscode-server/extensions/ms-python.python-2023.16.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "/workspaces/core/homeassistant/__main__.py", line 216, in <module>
    sys.exit(main())
  File "/workspaces/core/homeassistant/__main__.py", line 204, in main
    exit_code = runner.run(runtime_conf)
  File "/workspaces/core/homeassistant/runner.py", line 186, in run
    return loop.run_until_complete(setup_and_run_hass(runtime_config))
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 640, in run_until_complete
    self.run_forever()
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 607, in run_forever
    self._run_once()
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 1914, in _run_once
    handle._run()
  File "/usr/local/lib/python3.11/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/workspaces/core/homeassistant/helpers/event.py", line 1442, in _run_async_call_action
    hass.async_run_hass_job(job, time_tracker_utcnow())
  File "/workspaces/core/homeassistant/core.py", line 636, in async_run_hass_job
    hassjob.target(*args)
  File "/workspaces/core/homeassistant/helpers/event.py", line 1511, in interval_listener
    hass.async_run_hass_job(job, now)
  File "/workspaces/core/homeassistant/core.py", line 639, in async_run_hass_job
    return self.async_add_hass_job(hassjob, *args)
  File "/workspaces/core/homeassistant/core.py", line 542, in async_add_hass_job
    task = self.loop.run_in_executor(None, hassjob.target, *args)
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 828, in run_in_executor
    return futures.wrap_future(
  File "/usr/local/lib/python3.11/asyncio/futures.py", line 417, in wrap_future
    new_future = loop.create_future()
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 427, in create_future
    return futures.Future(loop=self)
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspaces/core/config/custom_components/shinobi/managers/websockets.py", line 541, in _check_triggers
    self._hass.async_create_task(self._async_check_triggers(now))
  File "/workspaces/core/homeassistant/core.py", line 569, in async_create_task
    task = self.loop.create_task(target, name=name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 436, in create_task
    task = tasks.Task(coro, loop=self, name=name, context=context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 763, in call_soon
    self._check_thread()
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 800, in _check_thread
    raise RuntimeError(
RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one
2023-09-25 08:36:00.688 ERROR (MainThread) [homeassistant] Error doing job: Task was destroyed but it is pending!:   File "/home/vscode/.vscode-server/extensions/ms-python.python-2023.16.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydev_bundle/pydev_monkey.py", line 1118, in __call__
    ret = self.original_func(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.11/threading.py", line 995, in _bootstrap
    self._bootstrap_inner()
  File "/usr/local/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.11/threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 83, in _worker
    work_item.run()
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/workspaces/core/config/custom_components/shinobi/managers/websockets.py", line 541, in _check_triggers
    self._hass.async_create_task(self._async_check_triggers(now))
  File "/workspaces/core/homeassistant/core.py", line 569, in async_create_task
    task = self.loop.create_task(target, name=name)
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 436, in create_task
    task = tasks.Task(coro, loop=self, name=name, context=context)

Simone

bdraco commented 1 year ago

https://github.com/elad-bar/ha-shinobi/blob/da7019b487f53ca9872434430f1740b2365571cc/custom_components/shinobi/managers/websockets.py#L539

This function needs to be decorated with @callback or it will run in the executor