fix: replace unsafe Arc mutation in PUT /api/budget with RwLock#782
Open
rager306 wants to merge 1 commit intoRightNow-AI:mainfrom
Open
fix: replace unsafe Arc mutation in PUT /api/budget with RwLock#782rager306 wants to merge 1 commit intoRightNow-AI:mainfrom
rager306 wants to merge 1 commit intoRightNow-AI:mainfrom
Conversation
PUT /api/budget casts &Arc<AppState> to *mut KernelConfig and mutates the budget fields through a raw pointer. This is unsound: AppState is shared across Tokio worker threads, so two concurrent PUT /api/budget requests cause a data race on the same memory location. Replace with Arc<tokio::sync::RwLock<BudgetConfig>> stored on AppState, initialized from kernel.config.budget at startup. All readers use .read().await and all writers use .write().await. No unsafe code remains in the budget update path. Fixes: data race / undefined behaviour under concurrent budget updates
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
PUT /api/budgetcurrently casts&Arc<AppState>to*mut KernelConfigand mutates budget fields through a raw pointer:This is unsound:
AppStateis wrapped inArcand shared across all Tokio worker threads. Two concurrentPUT /api/budgetrequests produce a data race on the same memory. The Rust compiler gives no guarantees for this pattern — the optimizer can reorder or elide the stores.Fix
Add
pub budget_config: Arc<tokio::sync::RwLock<BudgetConfig>>toAppState, initialized fromkernel.config.budgetat startup. Readers use.read().await, writer uses.write().await. Zerounsafecode in the budget update path.Testing
cargo build -p openfang-apipassesunsafeblocks introduced