Fix potential panic bugs in string slicing and server shutdown#410
Open
hobostay wants to merge 1 commit intoRightNow-AI:mainfrom
Open
Fix potential panic bugs in string slicing and server shutdown#410hobostay wants to merge 1 commit intoRightNow-AI:mainfrom
hobostay wants to merge 1 commit intoRightNow-AI:mainfrom
Conversation
This commit fixes several potential panic bugs and race conditions:
1. **Race condition in desktop server shutdown** (crates/openfang-desktop/src/server.rs):
- Added `AtomicBool` flag to track shutdown state
- Both `shutdown()` and `Drop` now use `compare_exchange` to ensure
shutdown only happens once, preventing race conditions where both
try to manipulate the same resources simultaneously
2. **String slicing panic** (crates/openfang-api/src/channel_bridge.rs):
- Added `truncate_str()` helper function to safely truncate strings
- Fixed 9 occurrences where `&str[..8]` could panic if string < 8 chars
- Lines affected: 354, 393, 408, 431, 491, 513, 545, 565, 609
3. **String slicing panic** (crates/openfang-runtime/src/tool_runner.rs):
- Fixed canvas filename generation where `&canvas_id[..8]` could panic
- Now safely handles strings shorter than 8 characters
These fixes prevent potential panics in production code when handling
UUIDs or other identifiers that might be shorter than expected.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes several potential panic bugs and race conditions found in the codebase:
1. Race Condition in Desktop Server Shutdown (
crates/openfang-desktop/src/server.rs)Problem: Both
shutdown()andDropimplementation tried to send shutdown signals and join threads, which could lead to race conditions when both try to manipulate the same resources simultaneously.Fix:
AtomicBoolflag (shutdown_initiated) to track shutdown stateshutdown()andDropnow usecompare_exchangeto ensure shutdown only happens once2. String Slicing Panic (
crates/openfang-api/src/channel_bridge.rs)Problem: Multiple locations used
&str[..8]to truncate UUID strings for display without checking if the string was at least 8 characters long. While UUIDs are always 36 characters, this pattern is unsafe and could cause panics if the data changes.Fix:
truncate_str()helper function that safely returns the full string if it's shorter than the requested length3. String Slicing Panic (
crates/openfang-runtime/src/tool_runner.rs)Problem: Canvas filename generation used
&canvas_id[..8]without checking length.Fix:
Test Plan
cargo build --workspacecargo test --workspaceType of Change
🤖 Generated with Claude Code