Fix crash of iOS FlipperNetworkPlugin (#563)

Summary:
Flipper's network plugin may cause crash on a frequency of about one time per 1~2 days(I think it depends on how many network request we send).
![IMG_3095](https://user-images.githubusercontent.com/24563710/65739973-cc540080-e119-11e9-9e6e-e4a925ecc63c.JPG)

  I assume the crash is caused by part of the code of HTTPParser is not thread-safe, and invoke it asynchronously may cause double-free crash. So I manually ask for HTTPBody synchronously, make the HTTPParser parse the request and cache them in advance, before any possible asynchronous invoking.

## Changelog
  Fix potential crash cause by network plugin.
Pull Request resolved: https://github.com/facebook/flipper/pull/563

Test Plan:
1. Run our App with Flipper integrated
2. Make sure FlipperNetworkPlugin is active
3. It may take days to reproduce the crash

Reviewed By: passy

Differential Revision: D17739327

Pulled By: priteshrnandgaonkar

fbshipit-source-id: e814145c346bd2da1d2f5f87b6a2f7e200f0bf2d
This commit is contained in:
LAgagggggg
2019-10-04 05:36:41 -07:00
committed by Facebook Github Bot
parent 6e6873a4f6
commit 0ba47d9a71

View File

@@ -274,6 +274,14 @@ didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask delegate:(id<NSUR
Method originalResume = class_getInstanceMethod(className, selector);
void (^swizzleBlock)(NSURLSessionTask *) = ^(NSURLSessionTask *slf) {
// iOS's internal HTTP parser finalization code is mysteriously not thread safe,
// invoke it asynchronously has a chance to cause a `double free` crash.
// This line below will ask for HTTPBody synchronously, make the HTTPParser parse the request and cache them in advance,
// After that the HTTPParser will be finalized,
// make sure other threads inspecting the request won't trigger a race to finalize the parser.
[slf.currentRequest HTTPBody];
[[FLEXNetworkObserver sharedObserver] URLSessionTaskWillResume:slf];
((void(*)(id, SEL))objc_msgSend)(slf, swizzledSelector);
};