Repository navigation
Commit dba6aba
authored
fix: Prevent losing a status message when redirecting Actor run logs (#1036)
### What
`ActorClient.call` built its two log redirectors inside a single `with`
statement:
```python
with run_client.get_status_message_watcher(to_logger=logger), run_client.get_streamed_log(to_logger=logger):
```
`with A(), B():` enters `A` before constructing `B`, so the status
watcher's polling thread was already running - and already logging into
`apify.<actor> runId:<id>` - when `get_streamed_log` called
`create_redirect_logger` on that same logger. That call reconfigures the
logger from scratch (`propagate = False`, `removeHandler`,
`addHandler`), so a status message emitted in that window went to a
logger that was mid-rebuild and was dropped.
Both redirectors are now constructed before either one starts polling,
which is what the async `call` already did.
### Why
This surfaced as a flaky unit test - [CI run
32853344220](https://github.com/apify/apify-client-python/actions/runs/32853344220/job/97819276512),
`test_actor_call_redirect_logs_to_default_logger_sync` on
`ubuntu-latest` / 3.13. `Status: RUNNING, Message: Initial message`
appeared in captured stderr but not in `caplog`, i.e. the record reached
the logger's own handler while `propagate` was `False`. Only the sync
test ever flaked, because the async `call` constructs both redirectors
up front and so has no such window.
The nondeterminism is in the client, not in the test: under real usage
the same window can silently drop a status message.
### Test
The new test pins the damaging interleaving with two events instead of
relying on thread scheduling, so it is deterministic in both directions
- it fails on `master` and passes with this change, at the same runtime
either way.
Verification: the new test fails without the `actor.py` change and
passes with it; full unit suite (911 passed), lint, type check and
docstring check all pass. The original flake does not reproduce
naturally on an 8-core machine (0 failures in 40 x
`tests/unit/test_logging.py` under `--numprocesses=16` before the
change), which is why the regression test forces the interleaving rather
than hammering for it.
*✍️ Drafted by Claude Code*1 parent 9876382 commit dba6aba
2 files changed
Lines changed: 71 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
371 | 371 | | |
372 | 372 | | |
373 | 373 | | |
374 | | - | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
375 | 381 | | |
376 | 382 | | |
377 | 383 | | |
| |||
868 | 874 | | |
869 | 875 | | |
870 | 876 | | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
871 | 880 | | |
872 | 881 | | |
873 | 882 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | | - | |
| 18 | + | |
| 19 | + | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| |||
24 | 26 | | |
25 | 27 | | |
26 | 28 | | |
| 29 | + | |
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
| |||
372 | 375 | | |
373 | 376 | | |
374 | 377 | | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
375 | 435 | | |
376 | 436 | | |
377 | 437 | | |
| |||
0 commit comments