You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: cli/knowledge.md
+41Lines changed: 41 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -630,3 +630,44 @@ Typing `@` scans the local `.agents` directory and surfaces agent `displayName`s
630
630
## Streaming Markdown Optimization
631
631
632
632
Streaming markdown renders as plain text until the message or agent finishes. This prevents scroll jitter that occurred when partial formatting changed line heights mid-stream.
633
+
634
+
## Bun Runtime Stability with React State Updates
635
+
636
+
**CRITICAL**: Bun can crash with segmentation faults when cascading React state updates occur rapidly, especially during error handling or network disconnections.
637
+
638
+
### Crash Signature
639
+
640
+
```
641
+
panic(main thread): Segmentation fault at address 0x10
642
+
oh no: Bun has crashed. This indicates a bug in Bun, not your code.
643
+
```
644
+
645
+
### Common Triggers
646
+
647
+
1. **Multiple simultaneous state updates** - When several async operations complete/fail at once
648
+
2. **Error handler state updates** - Calling setState() from error handlers that fire for multiple failures
649
+
3. **Network disconnection cascades** - When server dies while multiple operations are in progress
650
+
4. **Rapid useEffect re-triggers** - Effects that depend on frequently changing values
651
+
652
+
### General Guidelines
653
+
654
+
1. **Batch state updates when possible** - Use a single setState() call instead of multiple
655
+
2. **Be cautious in error handlers** - Avoid triggering state updates for every error; collect and batch instead
656
+
3. **Debounce frequent updates** - If state changes rapidly, add debouncing
657
+
4. **Test with multiple simultaneous failures** - Kill the server while operations are pending
658
+
5. **Avoid complex state logic in event handlers** - Keep SDK/network event handlers simple
659
+
6. **Use refs for values that don't need to trigger re-renders** - Reduces unnecessary effect runs
660
+
661
+
### Example Issue (November 2024)
662
+
663
+
When implementing automatic retry on reconnection, scheduling retries in both SDK error handlers and runState error handlers caused crashes when the server was killed while messages were queued. Each queued message would fail and call `setPendingRetryCount()`, triggering a cascade of state updates that crashed Bun.
664
+
665
+
**Solution**: Moved retry scheduling to a single, controlled location (the existing catch block) instead of adding redundant retry points in multiple error handlers.
666
+
667
+
### Key Takeaway
668
+
669
+
**Bun's React reconciler is less robust than Node.js when handling rapid state changes.** Code that works fine in Node.js may crash Bun under stress. Always test scenarios involving:
0 commit comments