Skip to content

Commit

Permalink
fix: fix the race condition when calling fileReader.readAsDataURL rig…
Browse files Browse the repository at this point in the history
…ht after new Blob(blobs)
  • Loading branch information
wood1986 committed Jun 29, 2022
1 parent 297b571 commit 099c5fb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
11 changes: 6 additions & 5 deletions Libraries/Blob/RCTBlobManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ @implementation RCTBlobManager
// Blobs should be thread safe since they are used from the websocket and networking module,
// make sure to use proper locking when accessing this.
NSMutableDictionary<NSString *, NSData *> *_blobs;
std::mutex _blobsMutex;
std::recursive_mutex _blobsMutex;

NSOperationQueue *_queue;
}
Expand All @@ -42,7 +42,7 @@ @implementation RCTBlobManager

- (void)initialize
{
std::lock_guard<std::mutex> lock(_blobsMutex);
std::lock_guard<std::recursive_mutex> lock(_blobsMutex);
_blobs = [NSMutableDictionary new];

facebook::react::RCTBlobCollector::install(self);
Expand Down Expand Up @@ -75,7 +75,7 @@ - (NSString *)store:(NSData *)data

- (void)store:(NSData *)data withId:(NSString *)blobId
{
std::lock_guard<std::mutex> lock(_blobsMutex);
std::lock_guard<std::recursive_mutex> lock(_blobsMutex);
_blobs[blobId] = data;
}

Expand All @@ -93,7 +93,7 @@ - (NSData *)resolve:(NSString *)blobId offset:(NSInteger)offset size:(NSInteger)
{
NSData *data;
{
std::lock_guard<std::mutex> lock(_blobsMutex);
std::lock_guard<std::recursive_mutex> lock(_blobsMutex);
data = _blobs[blobId];
}
if (!data) {
Expand Down Expand Up @@ -132,7 +132,7 @@ - (NSData *)resolveURL:(NSURL *)url

- (void)remove:(NSString *)blobId
{
std::lock_guard<std::mutex> lock(_blobsMutex);
std::lock_guard<std::recursive_mutex> lock(_blobsMutex);
[_blobs removeObjectForKey:blobId];
}

Expand Down Expand Up @@ -176,6 +176,7 @@ - (void)remove:(NSString *)blobId

RCT_EXPORT_METHOD(createFromParts:(NSArray<NSDictionary<NSString *, id> *> *)parts withId:(NSString *)blobId)
{
std::lock_guard<std::recursive_mutex> lock(_blobsMutex);
NSMutableData *data = [NSMutableData new];
for (NSDictionary<NSString *, id> *part in parts) {
NSString *type = [RCTConvert NSString:part[@"type"]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,30 +349,32 @@ public void sendOverSocket(ReadableMap blob, double idDouble) {

@Override
public void createFromParts(ReadableArray parts, String blobId) {
int totalBlobSize = 0;
ArrayList<byte[]> partList = new ArrayList<>(parts.size());
for (int i = 0; i < parts.size(); i++) {
ReadableMap part = parts.getMap(i);
switch (part.getString("type")) {
case "blob":
ReadableMap blob = part.getMap("data");
totalBlobSize += blob.getInt("size");
partList.add(i, resolve(blob));
break;
case "string":
byte[] bytes = part.getString("data").getBytes(Charset.forName("UTF-8"));
totalBlobSize += bytes.length;
partList.add(i, bytes);
break;
default:
throw new IllegalArgumentException("Invalid type for blob: " + part.getString("type"));
synchronized (mBlobs) {
int totalBlobSize = 0;
ArrayList<byte[]> partList = new ArrayList<>(parts.size());
for (int i = 0; i < parts.size(); i++) {
ReadableMap part = parts.getMap(i);
switch (part.getString("type")) {
case "blob":
ReadableMap blob = part.getMap("data");
totalBlobSize += blob.getInt("size");
partList.add(i, resolve(blob));
break;
case "string":
byte[] bytes = part.getString("data").getBytes(Charset.forName("UTF-8"));
totalBlobSize += bytes.length;
partList.add(i, bytes);
break;
default:
throw new IllegalArgumentException("Invalid type for blob: " + part.getString("type"));
}
}
ByteBuffer buffer = ByteBuffer.allocate(totalBlobSize);
for (byte[] bytes : partList) {
buffer.put(bytes);
}
store(buffer.array(), blobId);
}
ByteBuffer buffer = ByteBuffer.allocate(totalBlobSize);
for (byte[] bytes : partList) {
buffer.put(bytes);
}
store(buffer.array(), blobId);
}

@Override
Expand Down

0 comments on commit 099c5fb

Please sign in to comment.