Skip to content
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

Prevent ConcurrentModificationException in Before notify/breadcrumb callbacks #266

Merged
merged 4 commits into from
Mar 9, 2018
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
@@ -0,0 +1,74 @@
package com.bugsnag.android;

import android.support.annotation.NonNull;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Collection;

/**
* Ensures that if a callback is added or removed during iteration, a
* {@link java.util.ConcurrentModificationException} is not thrown
*/
@RunWith(AndroidJUnit4.class)
@SmallTest
public class ConcurrentCallbackTest {

private Client client;

@Before
public void setUp() throws Exception {
client = BugsnagTestUtils.generateClient();
}

@Test
public void testClientNotifyModification() throws Exception {
final Collection<BeforeNotify> beforeNotifyTasks = client.config.getBeforeNotifyTasks();
client.beforeNotify(new BeforeNotify() {
@Override
public boolean run(Error error) {
beforeNotifyTasks.add(new BeforeNotifySkeleton());
// modify the Set, when iterating to the next callback this should not crash
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails without the change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as it would modify the contents of the LinkedHashSet when an iterator has already been implicitly created in runBeforeNotifyTasks

}
});
client.beforeNotify(new BeforeNotifySkeleton());
client.notify(new RuntimeException());
}

@Test
public void testClientBreadcrumbModification() throws Exception {
final Collection<BeforeRecordBreadcrumb> breadcrumbTasks = client.config.getBeforeRecordBreadcrumbTasks();

client.beforeRecordBreadcrumb(new BeforeRecordBreadcrumb() {
@Override
public boolean shouldRecord(@NonNull Breadcrumb breadcrumb) {
breadcrumbTasks.add(new BeforeRecordBreadcrumbSkeleton());
// modify the Set, when iterating to the next callback this should not crash
return true;
}
});
client.beforeRecordBreadcrumb(new BeforeRecordBreadcrumbSkeleton());
client.leaveBreadcrumb("Whoops");
client.notify(new RuntimeException());
}

static class BeforeNotifySkeleton implements BeforeNotify {
@Override
public boolean run(Error error) {
return true;
}
}

static class BeforeRecordBreadcrumbSkeleton implements BeforeRecordBreadcrumb {
@Override
public boolean shouldRecord(@NonNull Breadcrumb breadcrumb) {
return true;
}
}

}
13 changes: 9 additions & 4 deletions sdk/src/main/java/com/bugsnag/android/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
* User-specified configuration storage object, contains information
Expand Down Expand Up @@ -50,9 +51,9 @@ public class Configuration extends Observable implements Observer {

@NonNull
private MetaData metaData;
private final Collection<BeforeNotify> beforeNotifyTasks = new LinkedHashSet<>();
private final Collection<BeforeNotify> beforeNotifyTasks = new ConcurrentLinkedQueue<>();
private final Collection<BeforeRecordBreadcrumb> beforeRecordBreadcrumbTasks
= new LinkedHashSet<>();
= new ConcurrentLinkedQueue<>();
private String codeBundleId;
private String notifierType;

Expand Down Expand Up @@ -547,7 +548,9 @@ protected boolean shouldIgnoreClass(String className) {
* @param beforeNotify the new before notify task
*/
protected void beforeNotify(BeforeNotify beforeNotify) {
this.beforeNotifyTasks.add(beforeNotify);
if (!beforeNotifyTasks.contains(beforeNotify)) {
beforeNotifyTasks.add(beforeNotify);
}
}

/**
Expand All @@ -556,7 +559,9 @@ protected void beforeNotify(BeforeNotify beforeNotify) {
* @param beforeRecordBreadcrumb the new before breadcrumb task
*/
protected void beforeRecordBreadcrumb(BeforeRecordBreadcrumb beforeRecordBreadcrumb) {
this.beforeRecordBreadcrumbTasks.add(beforeRecordBreadcrumb);
if (!beforeRecordBreadcrumbTasks.contains(beforeRecordBreadcrumb)) {
beforeRecordBreadcrumbTasks.add(beforeRecordBreadcrumb);
}
}

/**
Expand Down