Skip to content

Concurrent handling loop with semaphore-gated polling #32

@DaviddeBest-TNO

Description

@DaviddeBest-TNO

Parent

#27

What to build

Redesign start_handling_loop() to dispatch multiple handler invocations concurrently, gated by a configurable semaphore.

Concurrent polling model: The loop runs multiple concurrent poll-dispatch cycles. Each cycle: acquire the semaphore → await poll_ki_call() → on HANDLE, spawn an asyncio.create_task that runs the handler, posts the response back to the SC, and releases the semaphore. The loop immediately starts a new cycle (acquiring the semaphore again). This bounds both in-flight polls and running handlers to max_concurrent_handlers.

Configuration: start_handling_loop() accepts a max_concurrent_handlers: int = 10 parameter that sets the semaphore size.

Error handling: When a handler task raises an exception: log the error (including KI name and request context), post an empty binding set back to the SC (to prevent it from hanging), release the semaphore, and continue. The handling loop must not crash due to a single handler failure.

Graceful shutdown: When any poll returns PollResult.EXIT, stop issuing new polls and await all in-flight handler tasks to completion before returning from start_handling_loop(). No timeout — handlers run to completion.

Event loop reference: Store a reference to the running event loop (asyncio.get_running_loop()) on the KnowledgeBase instance when start_handling_loop() begins. This will be used by the sync bridge in a subsequent slice.

The loops parameter for limiting iteration count (used in testing) should be preserved with semantics adapted for the concurrent model.

Acceptance criteria

  • start_handling_loop() accepts max_concurrent_handlers parameter (default 10)
  • Multiple incoming KI calls are dispatched concurrently (not sequentially)
  • Concurrency is bounded by the semaphore — no more than max_concurrent_handlers in-flight
  • Handler exceptions are logged and do not crash the loop
  • On handler exception, an empty binding set is posted back to the SC
  • On EXIT signal, loop stops polling and waits for in-flight handlers to complete
  • Running event loop reference stored on KnowledgeBase
  • loops parameter still works for testing
  • Tests verify concurrent dispatch (e.g. two slow handlers overlap in time)
  • Tests verify error handling (handler throws, loop continues)
  • Tests verify graceful shutdown behavior
  • uv run ruff check . passes

Blocked by

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestready-for-agentIssue is ready for agent implementation

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions