Skip to content

perf(streaming): reuse synchronous stream worker threads with bounded idle executor - #226

Merged
MisterVVP merged 2 commits into
dev-optimize-streaming-response-latenciesfrom
dev-implement-reusable-synchronous-stream-worker-executor-jskguk
Aug 27, 2026
Merged

MisterVVP merged 2 commits into
dev-optimize-streaming-response-latenciesfrom
dev-implement-reusable-synchronous-stream-worker-executor-jskguk

Conversation

@MisterVVP

@MisterVVP MisterVVP commented Aug 26, 2026 •

Copy link
Copy Markdown
Owner

Motivation

  • Reduce per-stream OS-thread creation/destruction to lower synchronous streaming startup and high-concurrency first-event latency by reusing worker threads.

Description

  • Add an internal-only elastic StreamWorkerExecutor that immediately creates a worker when no idle worker is available and retains at most kMaximumRetainedIdleStreamWorkers idle workers (default 64).
  • Refactor StreamHandle private state to use a per-stream completion token (completion_mutex, completion_condition, execution_thread_id, completed) so handles wait on task completion instead of owning an OS thread, preserving Cancel(), IsActive(), move semantics and avoiding self-join deadlocks.
  • Wire the executor into HTTP+JSON and JSON-RPC synchronous streaming paths (StartSseStream / SendStreamingMessage / SubscribeTask) so sessions are executed on executor workers and per-stream completion is signalled on task exit; leave gRPC worker model unchanged.
  • Add deterministic unit tests covering sequential reuse, concurrent independence, bounded idle retention, and shutdown behavior, and update build/test targets and CMake accordingly.

@MisterVVP MisterVVP added the codex Automatic label added by codex agent. Should not be used label Aug 26, 2026 — with ChatGPT Codex Connector
@MisterVVP MisterVVP self-assigned this Aug 26, 2026
@MisterVVP MisterVVP added AI agent AI agent contribution performance Performance improvement related tasks based on benchmark results. Performance tests changes. and removed codex Automatic label added by codex agent. Should not be used labels Aug 26, 2026
…e-worker reservation (#227)

### Motivation
- Two regressions caused timeouts after the reusable synchronous
stream-worker change: gRPC `StreamHandle::Cancel()` inferred
executor-backed execution from `worker_.joinable()` leading to waits
that never complete after the owned worker was joined, and
`StreamWorkerExecutor::Submit()` could observe the same idle worker
multiple times and overcommit a single idle worker.
- The intent is to make execution model explicit and make idle-worker
assignment accurate without reverting the reusable synchronous
stream-worker optimization.

### Description
- Add an explicit `ExecutionMode` (enum) on `StreamHandle` and
initialize/preserve it on construction and moves, and use it to decide
whether to wait on `State::completion_condition` instead of inferring
from `std::thread::joinable()`; this makes repeated `Cancel()`
idempotent and avoids the gRPC destructor/blocking deadlock. (changes in
`include/a2a/client/client.h` and `src/client/client.cpp`).
- Fix idle-worker reservation in `StreamWorkerExecutor` by decrementing
`idle_workers_` on `Submit()` when assigning a pending task to an idle
worker and removing a redundant decrement in the worker loop so each
idle worker is reserved for at most one submitted task. (change in
`src/client/stream_worker_executor.cpp`).
- Extend a focused unit test to cover repeated `Cancel()` on gRPC
streams to prevent regressions. (change in
`tests/unit/grpc_transport_test.cpp`).
- Modified files: `include/a2a/client/client.h`,
`src/client/client.cpp`, `src/client/stream_worker_executor.cpp`, and
`tests/unit/grpc_transport_test.cpp`.
@MisterVVP
MisterVVP marked this pull request as ready for review August 27, 2026 19:21
@MisterVVP
MisterVVP merged commit f26c4fa into dev-optimize-streaming-response-latencies Aug 27, 2026
19 checks passed
@MisterVVP
MisterVVP deleted the dev-implement-reusable-synchronous-stream-worker-executor-jskguk branch August 27, 2026 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI agent AI agent contribution performance Performance improvement related tasks based on benchmark results. Performance tests changes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant