Skip to content
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
Expand Up @@ -15,6 +15,7 @@
import android.content.Context;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -32,6 +33,9 @@ public class BacktraceReactNative extends ReactContextBaseJavaModule {

private static final boolean nativeLibraryLoaded = loadNativeLibrary();

// The crash handler's start-up attributes outrank any later update of the same key in the uploaded report.
private static final Set<String> HANDLER_ATTRIBUTE_NAMES = new HashSet<>(Arrays.asList("guid", "error.type"));

public native void Crash();

private final Context context;
Expand Down Expand Up @@ -77,13 +81,13 @@ public Boolean initialize(String minidumpSubmissionUrl, String databasePath, Rea
}

Map<String, Object> attributes = readableAttributes != null ? readableAttributes.toHashMap() : new HashMap<String, Object>();
String[] keys = new String[attributes.size()];
String[] values = new String[attributes.size()];
int index = 0;
List<String> keys = new ArrayList<>();
List<String> values = new ArrayList<>();
for (Map.Entry<String, Object> attribute : attributes.entrySet()) {
keys[index] = attribute.getKey();
values[index] = stringValue(attribute.getValue());
index++;
if (HANDLER_ATTRIBUTE_NAMES.contains(attribute.getKey())) {
keys.add(attribute.getKey());
values.add(stringValue(attribute.getValue()));
}
}

List<String> attachments = stringList(attachmentPaths);
Expand All @@ -93,8 +97,8 @@ public Boolean initialize(String minidumpSubmissionUrl, String databasePath, Rea
minidumpSubmissionUrl,
databasePath,
crashHandlerConfiguration.getClassPath(),
keys,
values,
keys.toArray(new String[0]),
values.toArray(new String[0]),
attachments.toArray(new String[0]),
crashHandlerConfiguration.getCrashHandlerEnvironmentVariables(this.context.getApplicationInfo()).toArray(new String[0])
);
Expand All @@ -104,6 +108,12 @@ public Boolean initialize(String minidumpSubmissionUrl, String databasePath, Rea
return false;
}

for (Map.Entry<String, Object> attribute : attributes.entrySet()) {
if (!HANDLER_ATTRIBUTE_NAMES.contains(attribute.getKey())) {
BacktraceDatabase.addAttribute(attribute.getKey(), stringValue(attribute.getValue()));
}
}

this.registeredAttachments.addAll(attachments);
this.initialized = true;
return true;
Expand Down
1 change: 1 addition & 0 deletions packages/react-native/src/BacktraceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ export class BacktraceClient extends BacktraceCoreClient<BacktraceConfiguration>
}

private initializeNativeCrashReporter(): CrashReporter | undefined {
this._crashReporter?.dispose();
if (!this.options.database?.enable) {
return;
}
Expand Down
63 changes: 57 additions & 6 deletions packages/react-native/src/crashReporter/CrashReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ export class CrashReporter {
*/
private static initialized = false;

private static activeReporter?: CrashReporter;
private static nativeSubmissionUrl?: string;
private static nativeDatabasePath?: string;
private static nativeAttributeNames = new Set<string>();

public initialize(
submissionUrl: string,
databasePath: string,
attributes: Record<string, AttributeType>,
attachments: readonly BacktraceAttachment[],
): boolean {
const nativeDatabasePath = `${databasePath}/native`;
if (CrashReporter.initialized) {
warnFailure("native crash reporting keeps the first client's configuration");
return false;
return this.takeOver(submissionUrl, nativeDatabasePath, attributes, attachments);
}
if (!CrashReporter.BacktraceReactNative) {
warnFailure('native crash reporting is off, BacktraceReactNative is not linked');
Expand All @@ -36,7 +41,7 @@ export class CrashReporter {
return false;
}

const nativeDatabasePath = `${databasePath}/native`;
const nativeAttributes = this.convertAttributes(attributes);
try {
this._fileSystem.createDirSync(nativeDatabasePath);

Expand All @@ -45,7 +50,7 @@ export class CrashReporter {
submissionUrl,
nativeDatabasePath,
{
...this.convertAttributes(attributes),
...nativeAttributes,
'error.type': 'Crash',
},
this.convertAttachments(attachments),
Expand All @@ -59,16 +64,19 @@ export class CrashReporter {
return false;
}

this._enabled = true;
CrashReporter.initialized = true;
CrashReporter.nativeSubmissionUrl = submissionUrl;
CrashReporter.nativeDatabasePath = nativeDatabasePath;
CrashReporter.nativeAttributeNames = new Set(Object.keys(nativeAttributes));
this.enable();
return true;
}

public updateAttributes(attributes: Record<string, AttributeType>) {
if (!this._enabled) {
return;
}
this.update(() => CrashReporter.BacktraceReactNative.useAttributes(this.convertAttributes(attributes)));
this.useAttributes(this.convertAttributes(attributes));
}

public updateAttachments(attachments: readonly BacktraceAttachment[]) {
Expand Down Expand Up @@ -96,6 +104,49 @@ export class CrashReporter {

public dispose(): void {
this._enabled = false;
if (CrashReporter.activeReporter === this) {
CrashReporter.activeReporter = undefined;
}
}

private takeOver(
submissionUrl: string,
nativeDatabasePath: string,
attributes: Record<string, AttributeType>,
attachments: readonly BacktraceAttachment[],
): boolean {
if (CrashReporter.activeReporter) {
warnFailure('native crash reporting stays with the client that is still running, dispose it first');
return false;
}
if (
submissionUrl !== CrashReporter.nativeSubmissionUrl ||
nativeDatabasePath !== CrashReporter.nativeDatabasePath
) {
warnFailure(
"native crash reports keep the first client's submission url and database path until the app restarts",
);
}

const blankedPreviousAttributes = Object.fromEntries(
[...CrashReporter.nativeAttributeNames].map((name) => [name, '']),
);
this.enable();
this.useAttributes({ ...blankedPreviousAttributes, ...this.convertAttributes(attributes) });
this.updateAttachments(attachments);
return true;
}

private enable() {
this._enabled = true;
CrashReporter.activeReporter = this;
}

private useAttributes(attributes: Record<string, string>) {
for (const name of Object.keys(attributes)) {
CrashReporter.nativeAttributeNames.add(name);
}
this.update(() => CrashReporter.BacktraceReactNative.useAttributes(attributes));
}

private update(fn: () => void) {
Expand Down
Loading
Loading