Skip to content

Commit a46e612

Browse files
committed
fix: Fix detection of whether any instance is initialized
Closes: #674
1 parent 34e1e23 commit a46e612

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/apify/_actor.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/unit/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def prepare_test_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Callabl
6262
def _prepare_test_env() -> None:
6363
if hasattr(apify._actor.Actor, '__wrapped__'):
6464
delattr(apify._actor.Actor, '__wrapped__')
65-
apify._actor._ActorType._is_any_instance_initialized = False
65+
apify._actor._ActorType._initialized_instance_count = 0
6666

6767
# Set the environment variable for the local storage directory to the temporary path.
6868
monkeypatch.setenv(ApifyEnvVars.LOCAL_STORAGE_DIR, str(tmp_path))

0 commit comments

Comments
 (0)