@@ -98,8 +98,8 @@ async def main() -> None:
9898 _is_rebooting = False
9999 """Whether the Actor is currently rebooting."""
100100
101- _is_any_instance_initialized = False
102- """Whether any Actor instance was initialized ."""
101+ _initialized_instance_count = 0
102+ """Count of currently initialized Actor instances ."""
103103
104104 def __init__ (
105105 self ,
@@ -175,7 +175,7 @@ async def __aenter__(self) -> Self:
175175 self .log .debug ('Configuration initialized' )
176176
177177 # Warn about non-standard usage patterns.
178- if _ActorType ._is_any_instance_initialized :
178+ if self ._is_any_instance_initialized :
179179 self .log .warning ('Repeated Actor initialization detected - this is non-standard usage, proceed with care.' )
180180
181181 # Update the global Actor proxy to refer to this instance.
@@ -197,7 +197,7 @@ async def __aenter__(self) -> Self:
197197
198198 # Mark initialization as complete and update global state.
199199 self ._is_initialized = True
200- _ActorType ._is_any_instance_initialized = True
200+ _ActorType ._initialized_instance_count += 1
201201 return self
202202
203203 async def __aexit__ (
@@ -247,6 +247,9 @@ async def finalize() -> None:
247247 await asyncio .wait_for (finalize (), self ._cleanup_timeout .total_seconds ())
248248 self ._is_initialized = False
249249
250+ # Update global state - decrement instance count (ensure it doesn't go negative)
251+ _ActorType ._initialized_instance_count = max (0 , _ActorType ._initialized_instance_count - 1 )
252+
250253 if self ._exit_process :
251254 sys .exit (self .exit_code )
252255
@@ -389,6 +392,11 @@ def _storage_client(self) -> SmartApifyStorageClient:
389392 'awaiting `Actor.init`.'
390393 )
391394
395+ @property
396+ def _is_any_instance_initialized (self ) -> bool :
397+ """Whether any Actor instance is currently initialized."""
398+ return self ._initialized_instance_count > 0
399+
392400 async def init (self ) -> None :
393401 """Initialize the Actor without using context-manager syntax.
394402
0 commit comments