11from __future__ import annotations
22
33import logging
4- import sys
54from asyncio import Event , Task , create_task , gather
65from collections .abc import Callable
76from contextvars import ContextVar , Token
@@ -28,9 +27,7 @@ class _HookStack(Singleton): # nocov
2827 Life cycle hooks can be stored in a thread local or context variable depending
2928 on the platform."""
3029
31- _state : ThreadLocal [list [LifeCycleHook ]] | ContextVar [list [LifeCycleHook ]] = (
32- ThreadLocal (list ) if sys .platform == "emscripten" else ContextVar ("hook_state" )
33- )
30+ _state : ContextVar [list [LifeCycleHook ]] = ContextVar ("hook_state" )
3431
3532 def get (self ) -> list [LifeCycleHook ]:
3633 try :
@@ -268,5 +265,14 @@ def set_current(self) -> None:
268265
269266 def unset_current (self ) -> None :
270267 """Unset this hook as the active hook in this thread"""
271- if HOOK_STACK .get ().pop () is not self :
272- raise RuntimeError ("Hook stack is in an invalid state" ) # nocov
268+ hook_stack = HOOK_STACK .get ()
269+ if not hook_stack :
270+ raise RuntimeError ( # nocov
271+ "Attempting to unset current life cycle hook but it no longer exists!\n "
272+ "A separate process or thread may have deleted this component's hook stack!"
273+ )
274+ if hook_stack and hook_stack .pop () is not self :
275+ raise RuntimeError ( # nocov
276+ "Hook stack is in an invalid state\n "
277+ "A separate process or thread may have modified this component's hook stack!"
278+ )
0 commit comments