57 unwanted termination (#60)
- fix: prevent race condition crash during device stop/restart
Add delays to prevent CFRelease(NULL) crashes caused by concurrent
device operations between main thread and MultitouchSupport framework's
internal thread (mt_ThreadedMTEntry).
Root cause: When waking from sleep, restart() calls stop() on the main
thread while the framework's internal thread may still be processing
callbacks. This race condition causes the framework to access
deallocated resources.
Changes:
- DeviceMonitor.stop(): Separate callback unregistration from device
stopping with frameworkCleanupDelay (50ms) between them. This allows
the framework's internal thread to see unregistered callbacks before
we stop devices.
- MultitouchManager.restart(): Use async DispatchQueue.main.asyncAfter
with restartCleanupDelay (100ms) instead of blocking Thread.sleep.
This prevents UI freeze during wake-from-sleep while ensuring the
framework has time to complete cleanup.
Named constants with documentation added for both delay values to
improve maintainability.
Fixes #57
- test: add coverage for race condition prevention
Add 10 new tests to verify the race condition fix:
DeviceMonitorTests (5 tests):
- testStopIncludesDelayForFrameworkCleanup: Verify delay exists
- testRapidStartStopCyclesDoNotCrash: Rapid start/stop cycles
- testStopSeparatesCallbackUnregistrationFromDeviceStop: Two-phase stop
- testConcurrentStopDoesNotCrash: Concurrent access during stop
- testFrameworkCleanupDelayConstantExists: Verify constant is defined
MultitouchManagerTests (5 tests):
- testRestartUsesAsyncDelayForFrameworkCleanup: Verify async non-blocking
- testRestartCleanupDelayConstantExists: Verify constant is defined
- testRapidRestartCyclesDoNotCrash: Rapid restart cycles (wake scenarios)
- testRestartDuringActiveGestureDoesNotCrash: Restart during drag
- testRestartFromBackgroundThreadDoesNotCrash: Background thread restart
Updated existing restart tests to wait for async completion.
This fixes a race condition where specific timing of stop() during the async restart delay would fail to prevent the restart. It also debounces rapid restart calls to prevent multiple DeviceMonitor instances.
SHA256: 5d8cc073bf99c09cc47c072217c2d2ae78ffdafaf76c5093cebc105b3ebacff2