ndb: replace force unwraps with safe optionals in NdbTxn#3562
ndb: replace force unwraps with safe optionals in NdbTxn#3562alltheseas wants to merge 1 commit intodamus-io:masterfrom
Conversation
Replace force unwraps (`as!`) with safe optional binding (`as?`) in NdbTxn and SafeNdbTxn transaction inheritance logic. This prevents crashes when thread-local storage is in an unexpected state. Changes: - Move `ref_count` check into the `if let` conditional binding - Use already-bound `txn_generation` instead of re-reading with force unwrap - Simplify ref count increment to single expression The fix ensures transaction inheritance gracefully falls back to creating a new transaction if thread-local state is incomplete, rather than crashing on missing values. Changelog-Fixed: Fixed potential crash in NdbTxn when thread-local storage is incomplete Signed-off-by: alltheseas
📝 WalkthroughWalkthroughChanges to transaction initialization in NdbTxn.swift replace force-unwrapping with optional binding for ref_count retrieval and align generation assignment across both NdbTxn.init and SafeNdbTxn.new, maintaining existing control flow while improving safety. Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~5 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Closing in favor of consolidated Linux-style topic branches. This work will be audited against AGENTS.md requirements and cherry-picked into ordered topic branches. See #3624 for tracking. |
Summary
Replace force unwraps (
as!) with safe optional binding (as?) in NdbTxn and SafeNdbTxn transaction inheritance logic. This prevents crashes when thread-local storage is in an unexpected state.Changes:
ref_countcheck into theif letconditional bindingtxn_generationinstead of re-reading with force unwrapThe fix ensures transaction inheritance gracefully falls back to creating a new transaction if thread-local state is incomplete, rather than crashing on missing values.
Checklist
Standard PR Checklist
Closes:orFixes:tags in the commit messages wherever applicable, or made sure those are not needed. See Submitting patchesTest report
Device: macOS Simulator (iPhone 17 Pro)
iOS: 18.0
Damus: master + this PR
Setup: Unit test suite via
xcodebuild testSteps:
xcodebuild test -project damus.xcodeproj -scheme damus -destination 'platform=iOS Simulator,name=iPhone 17 Pro' -only-testing:damusTests/NdbTeststest_inherited_transactionspasses - confirms transaction inheritance with safe optional binding worksResults:
Testing directions for reviewers
NdbTeststarget -test_inherited_transactionsspecifically tests the transaction inheritance pathif letchainOther notes
The original code had:
The fix moves
ref_countinto the conditional binding and reuses the already-boundtxn_generation:Signed-off-by: alltheseas
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.