react-native/Libraries
Jose Pereira b805172034 Clear _handlers on RCTNetworking invalidation
Summary:
This PR fixes a bug where in `RCTNetworking` not all tasks/handlers were not being cleared when invalidating the class.

I came across this issue when writing some unit tests for my native plugins, sometimes a test would finish running (and the bridge invalidated), and only afterwards a callback from RCTNetworking would come, resulting in this exception:

```
2018-05-07 15:23:34.264494-0700 Guardian[73794:10710945] *** Assertion failure in -[RCTEventEmitter sendEventWithName:body:](), /Users/.../app/node_modules/react-native/React/Modules/RCTEventEmitter.m:41
2018-05-07 15:23:34.276505-0700 Guardian[73794:10710945] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error when sending event: didCompleteNetworkResponse with body: (
    2,
    cancelled,
    0
). Bridge is not set. This is probably because you've explicitly synthesized the bridge in RCTNetworking, even though it's inherited from RCTEventEmitter.'
*** First throw call stack:
(
	0   CoreFoundation                      0x000000010d5b21e6 __exceptionPreprocess + 294
	1   libobjc.A.dylib                     0x000000010be6f031 objc_exception_throw + 48
	2   CoreFoundation                      0x000000010d5b7472 +[NSException raise:format:arguments:] + 98
	3   Foundation                          0x000000010b94864f -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 165
	4   Guardian                            0x0000000106ff5227 -[RCTEventEmitter sendEventWithName:body:] + 567
	5   Guardian                            0x0000000106e9ebab __76-[RCTNetworking sendRequest:responseType:incrementalUpdates:responseSender:]_block_invoke.423 + 1115
	6   Guardian                            0x0000000106e8f48c __50-[RCTNetworkTask URLRequest:didCompleteWithError:]_block_invoke + 92
	7   Guardian                            0x0000000106e8ded1 -[RCTNetworkTask dispatchCallback:] + 113
	8   Guardian                            0x0000000106e8f37a -[RCTNetworkTask URLRequest:didCompleteWithError:] + 410
	9   Guardian                            0x0000000106ea1aa3 -[RCTHTTPRequestHandler URLSession:task:didCompleteWithError:] + 403
	10  CFNetwork                           0x000000010cf3a437 __51-[NSURLSession delegate_task:didCompleteWithError:]_block_invoke.207 + 80
	11  Foundation                          0x000000010b885363 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
	12  Foundation                          0x000000010b8851ca -[NSBlockOperation main] + 68
	13  Foundation                          0x000000010b8836b2 -[__NSOperationInternal _start:] + 766
	14  libdispatch.dylib                   0x0000000112457779 _dispatch_client_callout + 8
	15  libdispatch.dylib                   0x000000011245c931 _dispatch_block_invoke_direct + 317
	16  libdispatch.dylib                   0x0000000112457779 _dispatch_client_callout + 8
	17  libdispatch.dylib                   0x000000011245c931 _dispatch_block_invoke_direct + 317
	18  libdispatch.dylib                   0x000000011245c7d4 dispatch_block_perform + 109
	19  Foundation                          0x000000010b87f75b __NSOQSchedule_f + 337
	20  libdispatch.dylib                   0x0000000112457779 _dispatch_client_callout + 8
	21  libdispatch.dylib                   0x000000011245f1b2 _dispatch_queue_serial_drain + 735
	22  libdispatch.dylib                   0x000000011245f9af _dispatch_queue_invoke + 321
	23  libdispatch.dylib                   0x0000000112461cf8 _dispatch_root_queue_drain + 473
	24  libdispatch.dylib                   0x0000000112461ac1 _dispatch_worker_thread3 + 119
	25  libsystem_pthread.dylib             0x000000011297a169 _pthread_wqthread + 1387
	26  libsystem_pthread.dylib             0x0000000112979be9 start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
```

Bug can be reproduced by making a `XMLHttpRequest` (uses `RCTNetworking` internally) that takes a couple seconds to perform, and issuing a RCTBridge reload command in the meantime.

You can add the following code to the react-native template project,

```
  componentDidMount() {
    var oReq = new XMLHttpRequest();
    oReq.addEventListener("load", () => console.log('Finished'));
    oReq.open("GET", "https://www.dropbox.com/s/o01hz0chqvjafhv/file.bin?dl=1");
    oReq.send();
    console.log('Request is being performed...')
  }
```
In my case I download a 1MB file.
Run the project and reload the a couple times. Bug is triggered.

 [INTERNAL] [BUGFIX] [RCTNetworking] - Clear handlers and tasks on RCTNetworking invalidation
Closes https://github.com/facebook/react-native/pull/19169

Differential Revision: D8053070

Pulled By: hramos

fbshipit-source-id: d8af54fecd99173905363f962ffc638ef8b85082
2018-05-17 19:10:48 -07:00
..
ART Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
ActionSheetIOS Remove @providesModule from all modules 2018-04-25 07:37:10 -07:00
Alert Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
Animated Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
AppState Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
BatchedBridge Detach RN Bridge and CS 2018-05-11 10:57:53 -07:00
Blob Convert react-native-github/Libraries to let/const 2018-05-10 16:16:35 -07:00
BugReporting Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
CameraRoll Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
Color Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
Components Bring back TextInput.State, deprecate focusTextInput and blurTextInput 2018-05-17 11:26:22 -07:00
Core Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
EventEmitter Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
Experimental Flowtype SwipeableRow 2018-05-14 00:24:44 -07:00
Geolocation Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
Image Fix crash during Archiving project in IOS 2018-05-14 11:49:53 -07:00
Inspector Flowtype ListView 2018-05-14 00:24:44 -07:00
Interaction RN: Remove ReactPerf References 2018-05-15 13:16:57 -07:00
JSInspector Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
LayoutAnimation Remove @providesModule from all modules 2018-04-25 07:37:10 -07:00
Linking Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
LinkingIOS Update license headers for MIT license 2018-02-16 18:31:53 -08:00
Lists Flowtype ListView 2018-05-14 00:24:44 -07:00
Modal Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
NativeAnimation Fix tvOS build breakage in RCTAnimation Xcode project 2018-04-02 22:33:45 -07:00
Network Clear _handlers on RCTNetworking invalidation 2018-05-17 19:10:48 -07:00
Performance Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
PermissionsAndroid Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
PushNotificationIOS Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
RCTTest RN: Fix Type for ReactNative.NativeComponent (2/2) 2018-05-13 01:05:57 -07:00
ReactNative RN: Fix $FlowFixMe in AppContainer 2018-05-14 17:52:25 -07:00
Renderer React sync for revisions bde4b16...de84d5c 2018-05-15 16:48:49 -07:00
Sample Add missing copyright headers 2018-05-11 13:00:50 -07:00
Settings Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
Share Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
Storage Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
StyleSheet Flow Type ScrollView 2018-05-12 10:35:27 -07:00
SurfaceBackedComponent Update license headers for MIT license 2018-02-16 18:31:53 -08:00
SurfaceHostingComponent Update license headers for MIT license 2018-02-16 18:31:53 -08:00
Text iOS: fix up RNTesterPods 2018-05-16 14:14:41 -07:00
Types Remove @providesModule from all modules 2018-04-25 07:37:10 -07:00
Utilities Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
Vibration Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
WebSocket Reland prevent console logging on iOS 11.3+ within WebSocket 2018-05-11 14:30:54 -07:00
Wrapper Update additional license headers 2018-03-08 12:10:14 -08:00
fishhook Merge fishhook.xcodeproj in RCTWebSocket 2017-08-09 07:48:09 -07:00
polyfills Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
react-native Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
vendor Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
.npmignore npmignore: ignore tests and fixtures 2018-02-27 08:42:14 -08:00
Promise.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
UTFSequence.js Remove @providesModule from all modules 2018-04-25 07:37:10 -07:00
promiseRejectionIsError.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00