Commit 60ed504
committed
fix: prevent premature stream closure in EventConsumer grace period
The EventConsumer grace period logic could close streams prematurely when
final events were still in-transit through MainEventBusProcessor. This
manifested as intermittent test failures where the stream would close
before all events were delivered.
Root Cause:
- When agent execution completes, EventConsumer enters a grace period
- It polls the ChildQueue with 500ms timeout, allowing 3 consecutive
timeouts (1.5s total) before closing the stream
- The original logic only checked queue.size() == 0
- However, final events can be in-transit: MainQueue → MainEventBus →
MainEventBusProcessor → ChildQueue
- This timing window (typically <500ms) could result in premature closure
when the local queue was empty but the final event hadn't arrived yet
Solution:
- Added EventQueue.isAwaitingFinalEvent() method
- MainQueue calls child.expectFinalEvent() when enqueueing final events
- EventConsumer checks awaitingFinalEvent flag before starting timeout
counter: agentCompleted && queueSize == 0 && !awaitingFinal
- ChildQueue clears the flag only when a FINAL event is dequeued (not on
any event, to avoid clearing it too early when non-final events arrive)
- This ensures the grace period doesn't start counting down while a final
event is still being distributed
The fix handles both local execution (events available immediately) and
replicated scenarios (events may arrive via Kafka with delays).1 parent f668b77 commit 60ed504
2 files changed
Lines changed: 42 additions & 7 deletions
Lines changed: 12 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
85 | | - | |
86 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
87 | 88 | | |
88 | 89 | | |
89 | 90 | | |
| |||
94 | 95 | | |
95 | 96 | | |
96 | 97 | | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
97 | 102 | | |
98 | | - | |
| 103 | + | |
99 | 104 | | |
100 | 105 | | |
101 | 106 | | |
| |||
112 | 117 | | |
113 | 118 | | |
114 | 119 | | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
119 | 124 | | |
120 | 125 | | |
121 | 126 | | |
| |||
Lines changed: 30 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
304 | 304 | | |
305 | 305 | | |
306 | 306 | | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
307 | 326 | | |
308 | 327 | | |
309 | 328 | | |
| |||
735 | 754 | | |
736 | 755 | | |
737 | 756 | | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
738 | 763 | | |
739 | 764 | | |
740 | 765 | | |
| |||
757 | 782 | | |
758 | 783 | | |
759 | 784 | | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
760 | 790 | | |
761 | 791 | | |
762 | 792 | | |
| |||
0 commit comments