Commit 0d2734c
fix: Add callback INSIDE registerAndExecuteAgentAsync before starting CompletableFuture
The previous fix moved EventConsumer creation before registerAndExecuteAgentAsync,
but there was STILL a race condition:
Previous (broken) timeline:
1. EventConsumer consumer = new EventConsumer(queue)
2. EnhancedRunnable r = registerAndExecuteAgentAsync(...) ← Starts agent IMMEDIATELY
3. r.addDoneCallback(callback) ← TOO LATE if agent already completed
The registerAndExecuteAgentAsync method calls CompletableFuture.runAsync() which
starts the agent task immediately. If the agent completes very fast (validation
errors complete in microseconds), the whenComplete handler fires and calls
invokeDoneCallbacks() BEFORE line 3 can register the callback.
Fixed timeline:
1. EventConsumer consumer = new EventConsumer(queue)
2. registerAndExecuteAgentAsync(..., callback)
- Creates EnhancedRunnable
- Adds callback to runnable ← BEFORE starting!
- Starts CompletableFuture.runAsync()
- Returns runnable
3. Agent can complete any time, callback already registered
Changes:
- Add doneCallback parameter to registerAndExecuteAgentAsync()
- Register callback before CompletableFuture.runAsync()
- Update both callers (onMessageSend, onMessageSendStream)
This completely eliminates the race condition - callbacks are always registered
before the agent starts, regardless of execution speed.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>1 parent 1fca056 commit 0d2734c
1 file changed
Lines changed: 11 additions & 10 deletions
Lines changed: 11 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
442 | 442 | | |
443 | 443 | | |
444 | 444 | | |
445 | | - | |
446 | | - | |
447 | | - | |
| 445 | + | |
448 | 446 | | |
449 | 447 | | |
450 | | - | |
451 | | - | |
| 448 | + | |
452 | 449 | | |
453 | 450 | | |
454 | 451 | | |
| |||
626 | 623 | | |
627 | 624 | | |
628 | 625 | | |
629 | | - | |
630 | | - | |
| 626 | + | |
631 | 627 | | |
632 | 628 | | |
633 | | - | |
634 | | - | |
| 629 | + | |
635 | 630 | | |
636 | 631 | | |
637 | 632 | | |
| |||
869 | 864 | | |
870 | 865 | | |
871 | 866 | | |
| 867 | + | |
| 868 | + | |
872 | 869 | | |
873 | | - | |
| 870 | + | |
874 | 871 | | |
875 | 872 | | |
876 | 873 | | |
| |||
890 | 887 | | |
891 | 888 | | |
892 | 889 | | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
893 | 894 | | |
894 | 895 | | |
895 | 896 | | |
| |||
0 commit comments