mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 15:45:22 +00:00
Fix Noise session race condition with elegant per-peer actor serialization
- Use Kotlin coroutine actors for per-peer packet processing - Each peer gets dedicated actor that processes packets sequentially - Eliminates race conditions in session management without complex locking - Single surgical change in PacketProcessor - minimal, maintainable - Leverages Kotlin's native concurrency primitives
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
# android freezing cdk-ffi lightning memory
|
||||
## Analysis of Android Invoice Creation Freeze Issue
|
||||
|
||||
### Problem Summary
|
||||
- **Issue**: App freezes entirely when creating Lightning invoices on some Android devices
|
||||
- **Symptoms**: Mint quote is created and persisted, but UI becomes unresponsive
|
||||
- **Inconsistency**: Only affects some Android devices, not others
|
||||
|
||||
### Key Findings from Code Analysis
|
||||
|
||||
#### 1. **Large CDK-FFI Native Library (Most Likely Culprit)**
|
||||
- `libcdk_ffi.so` is **16.6MB** (ARM64) and **15.8MB** (x86_64)
|
||||
- This is an extremely large native library that could cause:
|
||||
- **Memory pressure** on devices with limited RAM
|
||||
- **Loading delays** that freeze the main thread
|
||||
- **Device-specific compatibility issues** with different Android versions/manufacturers
|
||||
|
||||
#### 2. **Potential Main Thread Blocking Operations**
|
||||
```kotlin
|
||||
// In CashuService.createMintQuote - runs on Dispatchers.IO but may still block
|
||||
val ffiMintQuote = wallet!!.mintQuote(
|
||||
amount = FfiAmount(amount.toULong()),
|
||||
description = description ?: "Cashu mint"
|
||||
)
|
||||
```
|
||||
- CDK-FFI calls (`wallet!!.mintQuote()`) could involve:
|
||||
- **Network operations** to the mint
|
||||
- **Cryptographic computations**
|
||||
- **Database writes** to local store
|
||||
- **Memory allocations** for large data structures
|
||||
|
||||
#### 3. **Device Architecture Compatibility**
|
||||
- CDK-FFI may have compatibility issues with specific:
|
||||
- **Android versions** (older/newer Android APIs)
|
||||
- **Manufacturer customizations** (Samsung, Xiaomi, OnePlus, etc.)
|
||||
- **ARM/x86 architecture variations**
|
||||
- **Memory management differences** between OEMs
|
||||
|
||||
#### 4. **QR Code Generation Timing**
|
||||
```kotlin
|
||||
// In ReceiveLightningDialog - QR code generated immediately after quote creation
|
||||
val qrCodeBitMatrix = remember(text) {
|
||||
try {
|
||||
val sizePx = with(density) { 512.dp.toPx() }.toInt() // 512px QR code
|
||||
val writer = QRCodeWriter()
|
||||
writer.encode(text, BarcodeFormat.QR_CODE, sizePx, sizePx)
|
||||
} catch (e: WriterException) { null }
|
||||
}
|
||||
```
|
||||
- Large QR code generation (512px) happens on main thread
|
||||
- Combined with Lightning request string could be computationally expensive
|
||||
|
||||
#### 5. **Memory-Intensive Operations Sequence**
|
||||
1. CDK-FFI wallet initialization (if needed)
|
||||
2. Network call to mint for quote creation
|
||||
3. Cryptographic operations for invoice generation
|
||||
4. Database write to persist quote
|
||||
5. UI update to show loading state
|
||||
6. QR code generation for Lightning invoice
|
||||
7. UI composition updates
|
||||
|
||||
### Recommended Solutions
|
||||
|
||||
#### **Immediate Fixes:**
|
||||
|
||||
1. **Add Timeout Protection**
|
||||
```kotlin
|
||||
// Add timeout to CDK operations
|
||||
withTimeout(30000) { // 30 second timeout
|
||||
wallet!!.mintQuote(amount, description)
|
||||
}
|
||||
```
|
||||
|
||||
2. **Move QR Code Generation Off Main Thread**
|
||||
```kotlin
|
||||
// Generate QR code in background
|
||||
LaunchedEffect(currentMintQuote?.request) {
|
||||
// Generate QR in background coroutine
|
||||
}
|
||||
```
|
||||
|
||||
3. **Add Device Memory Checks**
|
||||
```kotlin
|
||||
// Check available memory before CDK operations
|
||||
val runtime = Runtime.getRuntime()
|
||||
val availableMemory = runtime.maxMemory() - runtime.totalMemory()
|
||||
if (availableMemory < 50_000_000) { // 50MB threshold
|
||||
// Show warning or use fallback
|
||||
}
|
||||
```
|
||||
|
||||
#### **Long-term Solutions:**
|
||||
|
||||
1. **Implement CDK Availability Detection**
|
||||
- Check if CDK-FFI loads successfully on app start
|
||||
- Fall back to mock/demo mode on incompatible devices
|
||||
- Show user-friendly error messages
|
||||
|
||||
2. **Add Progressive Loading**
|
||||
- Show spinner immediately when creating invoice
|
||||
- Update UI incrementally as operations complete
|
||||
- Provide cancellation option for long operations
|
||||
|
||||
3. **Device-Specific Optimizations**
|
||||
- Detect problematic device models
|
||||
- Adjust QR code resolution based on device capabilities
|
||||
- Implement device-specific CDK initialization parameters
|
||||
|
||||
Reference in New Issue
Block a user