Skip to content

Commit 3073ea5

Browse files
committed
Update knowledge.md
1 parent f149758 commit 3073ea5

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

cli/knowledge.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,44 @@ Typing `@` scans the local `.agents` directory and surfaces agent `displayName`s
630630
## Streaming Markdown Optimization
631631

632632
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:
670+
- Multiple simultaneous async operations
671+
- Network failures with pending operations
672+
- Error cascades
673+
- Rapid state transitions

0 commit comments

Comments
 (0)