Fix thread safety of RNGestureHandlerModule
methods
#1173
Merged
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.
Description
Fixes #1169.
Another instance of changing the handler state outside the UI thread. In this case, calling
handler.cancel()
which callsmOrchestrator.onHandlerStateChange()
interfered withscheduleFinishedHandlersCleanup()
method which callshandler.reset()
and setsmOrchestrator
tonull
.This may happen when threads interleave in the way described below:
RNGestureHandlerModule.dropGestureHandler
is called (directly or inattachGestureHandler()
; it's called on native modules thread)onStateChange
event on UI threadmHandlingChangeSemaphore
is zero and call toscheduleFinishedHandlersCleanup()
causescleanupFinishedHandlers()
to reset handlers and clearmOrchestrator
moveToState
which crashes the appThis may show when
RNGestureHandlerModule.attachGestureHandler()
orRNGestureHandlerModule.dropGestureHandler()
is rapidly called from JS (e.g. when frequently unmounting or updating handler component).After checking the call hierarchy of
GestureHandler.moveToState()
it seems like this is the last occurrence of a native-module-calling-UI-only-method problem.Example
Fast clicking on the TapGestureHandler crashes with error "Expected to run on UI thread", introduced in #1171. After applying this patch, the error disappears.