Repository navigation
Commit a016002
fix(server): run follow-up messages in the sender's contextvars (a2aproject#1317)
# Description
`ActiveTask` creates its producer once, during the first request for a
task. Later messages on the same task are queued to that producer, so
`AgentExecutor.execute()` ran with the **first** request's contextvars.
Per-request values set by ASGI middleware or a
`ServerCallContextBuilder` (auth tokens, tenant identity, tracing) were
stale on every follow-up turn, for example a reply after
`input-required`.
This change captures `contextvars.copy_context()` in
`enqueue_request()`, which runs in the sender's request, and runs
`execute()` in that context as a child task. Awaiting the child keeps
cancellation working, because cancelling the producer cancels the child.
`Context.run(asyncio.create_task, ...)` works on Python 3.10.
Only `execute()` changes context. Task-store writes and push
notifications still run in the producer's context, as before.
Tested:
- New `test_follow_up_message_runs_in_sender_contextvars`, for both
`on_message_send` and `on_message_send_stream`. It fails on `main` and
passes with this change.
- The reproduction in a2aproject#1316 prints `request-2` for the follow-up.
- `./scripts/lint.sh` passes. `uv run pytest --cov=src` gives 2223
passed, 93% coverage.
`tests/integration/cross_version/client_server/test_client_server.py`
was deselected because its server fails to start in my local environment
on `main` too.
- [x] Follow the [`CONTRIBUTING`
Guide](https://github.com/a2aproject/a2a-python/blob/main/CONTRIBUTING.md).
- [x] Make your Pull Request title in the
<https://www.conventionalcommits.org/> specification.
- [x] Ensure the tests and linter pass (Run `bash scripts/format.sh`
from the repository root to format)
- [x] Appropriate docs were updated (if necessary)
Fixes a2aproject#1316 🦕
Co-authored-by: mykytanetipa <mykytanetipa@google.com>1 parent 59dcbfb commit a016002
2 files changed
Lines changed: 105 additions & 7 deletions
File tree
- src/a2a/server/agent_execution
- tests/server/request_handlers
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
468 | 469 | | |
469 | 470 | | |
470 | 471 | | |
471 | | - | |
472 | | - | |
473 | | - | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
474 | 475 | | |
475 | 476 | | |
476 | 477 | | |
| |||
480 | 481 | | |
481 | 482 | | |
482 | 483 | | |
483 | | - | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
484 | 490 | | |
485 | | - | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
486 | 494 | | |
487 | 495 | | |
488 | 496 | | |
| |||
573 | 581 | | |
574 | 582 | | |
575 | 583 | | |
| 584 | + | |
576 | 585 | | |
577 | 586 | | |
578 | 587 | | |
| |||
607 | 616 | | |
608 | 617 | | |
609 | 618 | | |
610 | | - | |
611 | | - | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
612 | 625 | | |
613 | 626 | | |
614 | 627 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
2443 | 2444 | | |
2444 | 2445 | | |
2445 | 2446 | | |
| 2447 | + | |
| 2448 | + | |
| 2449 | + | |
| 2450 | + | |
| 2451 | + | |
| 2452 | + | |
| 2453 | + | |
| 2454 | + | |
| 2455 | + | |
| 2456 | + | |
| 2457 | + | |
| 2458 | + | |
| 2459 | + | |
| 2460 | + | |
| 2461 | + | |
| 2462 | + | |
| 2463 | + | |
| 2464 | + | |
| 2465 | + | |
| 2466 | + | |
| 2467 | + | |
| 2468 | + | |
| 2469 | + | |
| 2470 | + | |
| 2471 | + | |
| 2472 | + | |
| 2473 | + | |
| 2474 | + | |
| 2475 | + | |
| 2476 | + | |
| 2477 | + | |
| 2478 | + | |
| 2479 | + | |
| 2480 | + | |
| 2481 | + | |
| 2482 | + | |
| 2483 | + | |
| 2484 | + | |
| 2485 | + | |
| 2486 | + | |
| 2487 | + | |
| 2488 | + | |
| 2489 | + | |
| 2490 | + | |
| 2491 | + | |
| 2492 | + | |
| 2493 | + | |
| 2494 | + | |
| 2495 | + | |
| 2496 | + | |
| 2497 | + | |
| 2498 | + | |
| 2499 | + | |
| 2500 | + | |
| 2501 | + | |
| 2502 | + | |
| 2503 | + | |
| 2504 | + | |
| 2505 | + | |
| 2506 | + | |
| 2507 | + | |
| 2508 | + | |
| 2509 | + | |
| 2510 | + | |
| 2511 | + | |
| 2512 | + | |
| 2513 | + | |
| 2514 | + | |
| 2515 | + | |
| 2516 | + | |
| 2517 | + | |
| 2518 | + | |
| 2519 | + | |
| 2520 | + | |
| 2521 | + | |
| 2522 | + | |
| 2523 | + | |
| 2524 | + | |
| 2525 | + | |
| 2526 | + | |
| 2527 | + | |
| 2528 | + | |
| 2529 | + | |
| 2530 | + | |
0 commit comments