Skip to content

Conversation

@pushpender-singh-ap
Copy link
Owner

@pushpender-singh-ap pushpender-singh-ap commented Oct 17, 2025

🎯 Overview

This PR introduces comprehensive thread-safety improvements using modern concurrency patterns - Swift Actors for iOS and Kotlin atomic operations with synchronized locks for Android.

🔄 What Changed

iOS Improvements

  • Swift Actors: Introduced CameraSessionActor and CallbackActor for isolated state management
  • Async/Await: Migrated to async patterns for non-blocking operations
  • Race Condition Fixes: Eliminated check-then-act race conditions with atomic operations
  • Memory Leak Prevention: Added proper cleanup in CameraView deinit
  • TurboModule Migration: Removed RCTEventEmitter inheritance in favor of NativeReactNativeScannerSpecBase

Android Improvements

  • Atomic Operations: Replaced @Synchronized with AtomicBoolean and AtomicReference
  • ReentrantLock: Added fine-grained locking for camera binding operations
  • Executor Management: Improved lifecycle with proper shutdown and recreation logic
  • Thread-Safe Scanning: Lock-free scanning flag with CAS operations
  • Choreographer Cleanup: Fixed memory leaks in ReactNativeScannerView

TypeScript/JavaScript

  • CodegenTypes.EventEmitter: Migrated from manual event listeners to type-safe Codegen events
  • Simplified API: Removed manual addListener/removeListeners methods
  • Type Safety: Better event type definitions with BarcodeScannedEvent

📊 Technical Details

Thread Safety Patterns

Platform Pattern Purpose
iOS Swift Actors Isolated state management
iOS Async/Await Non-blocking operations
Android AtomicBoolean/AtomicReference Lock-free flag operations
Android ReentrantLock Camera binding synchronization
Android Volatile Visibility guarantees

🐛 Bugs Fixed

  • ❌ Race condition in startScanning() between permission check and flag set
  • ❌ Concurrent camera binding causing IllegalStateException
  • ❌ Memory leaks from Choreographer callbacks
  • ❌ Executor shutdown during active scanning
  • ❌ Session interruption handling race conditions
  • ❌ Preview layer binding timing issues

🔧 Migration Guide

Breaking Changes

  1. Event Handling: No longer uses NativeEventEmitter - uses Codegen events automatically
  2. iOS Session Access: currentSession() replaced with async getCurrentSession(completion:)

Before (v1.x):

import { NativeEventEmitter } from 'react-native';

const eventEmitter = new NativeEventEmitter(BarcodeScanner);
eventEmitter.addListener('onBarcodeScanned', callback);

After (v2.0.0):

// Handled automatically by the API
BarcodeScanner.startScanning(callback);

📈 Performance Impact

  • Reduced Lock Contention: Atomic operations eliminate synchronized method overhead
  • Non-Blocking iOS: Async patterns prevent main thread blocking
  • Memory Efficiency: Proper cleanup prevents accumulation

📝 Documentation

  • Updated README with thread-safety features
  • Added inline code documentation for complex concurrency patterns

…atomic operations

- Add Swift Actor-based isolation for camera session and callback management
- Implement atomic operations (AtomicBoolean, AtomicReference) in Android CameraManager
- Add ReentrantLock synchronization for camera binding operations
- Fix race conditions in scanning start/stop operations
- Improve executor lifecycle management with proper shutdown handling
- Add Choreographer callback cleanup to prevent memory leaks in ReactNativeScannerView
- Migrate from EventEmitter to CodegenTypes.EventEmitter for type-safe events
- Update iOS CameraManager to use async/await patterns
- Remove deprecated RCTEventEmitter inheritance in favor of TurboModule base
- Update README with thread-safety features documentation
@pushpender-singh-ap pushpender-singh-ap self-assigned this Oct 17, 2025
@pushpender-singh-ap pushpender-singh-ap merged commit 206abf0 into main Oct 17, 2025
5 of 6 checks passed
@pushpender-singh-ap pushpender-singh-ap deleted the feat/thread-safety-concurrency-improvements branch October 17, 2025 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant