Skip to content

Fix ConcurrentModificationException while calling emitBufferedMarks in Android #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.oblador.performance;

import android.os.Build;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Arguments;
Expand All @@ -14,15 +12,18 @@
import com.facebook.react.turbomodule.core.interfaces.TurboModule;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

// Should extend NativeRNPerformanceManagerSpec when codegen for old architecture is solved
public class PerformanceModule extends ReactContextBaseJavaModule implements TurboModule, RNPerformance.MarkerListener {
public static final String PERFORMANCE_MODULE = "RNPerformanceManager";
public static final String BRIDGE_SETUP_START = "bridgeSetupStart";

private static boolean eventsBuffered = true;
private static final List<PerformanceEntry> markBuffer = new ArrayList<>();
private static final Queue<PerformanceEntry> markBuffer = new ConcurrentLinkedQueue<>();
private static boolean didEmit = false;

public PerformanceModule(@NonNull final ReactApplicationContext reactContext) {
Expand Down Expand Up @@ -88,13 +89,11 @@ public static void setupListener() {
private static void clearMarkBuffer() {
RNPerformance.getInstance().clearEphermalEntries();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
markBuffer.removeIf(PerformanceEntry::isEphemeral);
} else {
for (PerformanceEntry entry : markBuffer) {
if (entry.isEphemeral()) {
markBuffer.remove(entry);
}
Iterator<PerformanceEntry> iterator = markBuffer.iterator();
while (iterator.hasNext()) {
PerformanceEntry entry = iterator.next();
if (entry.isEphemeral()) {
iterator.remove();
}
}
}
Expand Down Expand Up @@ -156,14 +155,18 @@ private static void addMark(PerformanceEntry entry) {

private void emitBufferedMarks() {
didEmit = true;
for (PerformanceEntry entry : markBuffer) {
Iterator<PerformanceEntry> iterator = markBuffer.iterator();
while (iterator.hasNext()) {
PerformanceEntry entry = iterator.next();
emitMark(entry);
}
emitNativeBufferedMarks();
}

private void emitNativeBufferedMarks() {
for (PerformanceEntry entry : RNPerformance.getInstance().getEntries()) {
Iterator<PerformanceEntry> iterator = RNPerformance.getInstance().getEntries().iterator();
while (iterator.hasNext()) {
PerformanceEntry entry = iterator.next();
emitMark(entry);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.oblador.performance;

import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.facebook.proguard.annotations.DoNotStrip;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;

public class RNPerformance {
Expand All @@ -28,7 +32,7 @@ interface MarkerListener {
}

private static final List<MarkerListener> sListeners = new CopyOnWriteArrayList<>();
private final List<PerformanceEntry> entries = new CopyOnWriteArrayList<>();
private final Queue<PerformanceEntry> entries = new ConcurrentLinkedQueue<>();

@DoNotStrip
protected void addListener(MarkerListener listener) {
Expand Down Expand Up @@ -105,7 +109,8 @@ private void emitMark(@NonNull PerformanceEntry entry) {
}
}

protected @NonNull List<PerformanceEntry> getEntries() {
protected @NonNull
Queue<PerformanceEntry> getEntries() {
return entries;
}

Expand All @@ -114,13 +119,11 @@ protected void clearEntries() {
}

protected void clearEntries(String name) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
entries.removeIf((entry) -> entry.getName().equals(name));
} else {
for (PerformanceEntry entry : entries) {
if (entry.getName().equals(name)) {
entries.remove(entry);
}
Iterator<PerformanceEntry> iterator = entries.iterator();
while (iterator.hasNext()) {
PerformanceEntry entry = iterator.next();
if (entry.getName().equals(name)) {
iterator.remove();
}
}
}
Expand All @@ -130,13 +133,11 @@ protected void clearEphermalEntries() {
return;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
entries.removeIf(PerformanceEntry::isEphemeral);
} else {
for (PerformanceEntry entry : entries) {
if (entry.isEphemeral()) {
entries.remove(entry);
}
Iterator<PerformanceEntry> iterator = entries.iterator();
while (iterator.hasNext()) {
PerformanceEntry entry = iterator.next();
if (entry.isEphemeral()) {
iterator.remove();
}
}
}
Expand Down