Skip to content

Fix race condition in SequenceToPaginatedResponseAdapter timeout eviction#72

Merged
nerzhulart merged 2 commits intoagentclientprotocol:masterfrom
fscarponi:fabrizio.scarponi/fix-race-in-squence-to-paginated-response-adapter
Feb 27, 2026
Merged

Fix race condition in SequenceToPaginatedResponseAdapter timeout eviction#72
nerzhulart merged 2 commits intoagentclientprotocol:masterfrom
fscarponi:fabrizio.scarponi/fix-race-in-squence-to-paginated-response-adapter

Conversation

@fscarponi
Copy link
Copy Markdown
Contributor

Problem

SequenceToPaginatedResponseAdapter had a race condition in cursor eviction. When a new cursor was created, the timeout job was launched immediately via launch { ... }, but the cursor was stored in the map only after the launch. With very short orphanedIteratorsEvictionTimeout values, the timeout job could fire and call map.remove(cursor) before map.put(cursor, ...) had executed. This left the cursor permanently in the map with no eviction job, effectively leaking the iterator.

Fix

Changed the timeout job to use CoroutineStart.LAZY so it is created but not started until after the cursor is stored in the map. The job is then explicitly started with timeoutJob.start(), guaranteeing the correct ordering:

  1. Create timeout job (lazy, does not run yet)
  2. Store cursor + iterator + job in the map
  3. Start the timeout job

This eliminates the race condition regardless of how short the eviction timeout is

@fscarponi fscarponi marked this pull request as ready for review February 27, 2026 16:02
@nerzhulart nerzhulart self-requested a review February 27, 2026 16:03
@fscarponi fscarponi force-pushed the fabrizio.scarponi/fix-race-in-squence-to-paginated-response-adapter branch from c5d00b6 to 2bbc639 Compare February 27, 2026 16:07
@nerzhulart nerzhulart merged commit 11ecde2 into agentclientprotocol:master Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants