Skip to content

Commit 4d520ba

Browse files
fix(mobile): coalesce crash diagnostics and clean failed writes
1 parent 2d133f6 commit 4d520ba

3 files changed

Lines changed: 60 additions & 15 deletions

File tree

‎apps/mobile/src/features/diagnostics/crash-log-model.test.ts‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe("formatStartupCrashReport", () => {
156156

157157
it("says so when nothing was recorded", () => {
158158
expect(formatStartupCrashReport([], { version: "1.1.1", build: "56" })).toBe(
159-
"T3 Code 1.1.1 (56)\nNo startup crashes recorded.",
159+
"T3 Code 1.1.1 (56)\nNo crashes recorded.",
160160
);
161161
});
162162
});
@@ -224,18 +224,41 @@ describe("persisted fatal JS diagnostics", () => {
224224
// Only disk survives a fresh module graph; recovery must not rely on logger memory.
225225
vi.resetModules();
226226
const { readCrashRecords } = await import("./crash-log-model");
227-
const records = readCrashRecords([FATAL]);
227+
const startupDetail = [
228+
"Domain: RCTErrorDomain",
229+
`Description: Unhandled JS Exception: Error: ${fatal.message}`,
230+
"",
231+
"This error is located at:",
232+
` at NativeThreadBoundary (${BUNDLE}:88:9)`,
233+
].join("\n");
234+
const nearbyTimestamp = capturedAt.getTime() + 50;
235+
const records = readCrashRecords([
236+
FATAL,
237+
{
238+
...FATAL,
239+
timestamp: capturedAt.getTime() + 33,
240+
message: `ErrorRecovery fatal exception: ${startupDetail}`,
241+
},
242+
{ ...FATAL, timestamp: nearbyTimestamp },
243+
]);
228244

229-
expect(records).toHaveLength(2);
245+
expect(records).toHaveLength(3);
230246
expect(records.map((record) => record.timestamp)).toEqual([
247+
nearbyTimestamp,
231248
capturedAt.getTime(),
232249
FATAL.timestamp,
233250
]);
234-
expect(records[0]?.description).toContain(fatal.message);
235-
expect(records[0]?.detail).toContain(fatal.stack);
251+
expect(records.map((record) => record.description)).toEqual([
252+
"TypeError: Cannot read property 'defaultModelSelection' of null",
253+
`Error: ${fatal.message}`,
254+
"TypeError: Cannot read property 'defaultModelSelection' of null",
255+
]);
256+
expect(records[1]?.detail).toContain(fatal.stack);
257+
expect(records[1]?.detail).toContain(startupDetail);
236258
const report = formatStartupCrashReport(records, { version: "1.1.1", build: "56" });
237259
expect(report).toContain(capturedAt.toISOString());
238260
expect(report).toContain(fatal.stack);
261+
expect(report).toContain(startupDetail);
239262
expect(report).toContain("defaultModelSelection");
240263
expect(report).toContain("at NewTaskFlowProvider");
241264
});
@@ -258,7 +281,7 @@ describe("persisted fatal JS diagnostics", () => {
258281
expect(previous).toHaveBeenLastCalledWith(fatal, true);
259282
expect(previous.mock.calls.at(-1)?.[0]).toBe(fatal);
260283
await Promise.allSettled(disk.pendingWrites);
261-
expect([...disk.files.values()].every((content) => content === "")).toBe(true);
284+
expect(disk.files.size).toBe(0);
262285
}
263286
expect(previous).toHaveBeenCalledTimes(3);
264287
});

‎apps/mobile/src/features/diagnostics/crash-log-model.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,22 @@ export function readCrashRecords(
5858
detail: record.stack ? `${description}\n${record.stack}` : description,
5959
};
6060
});
61-
return [...localRecords, ...parseStartupCrashRecords(entries)].sort(
62-
(left, right) => right.timestamp - left.timestamp,
63-
);
61+
const records: StartupCrashRecord[] = [...localRecords];
62+
for (const startupRecord of parseStartupCrashRecords(entries)) {
63+
// Expo's native launch handler can record the same fatal error after ErrorUtils.
64+
const localRecord = localRecords.find(
65+
(record) =>
66+
record.description === startupRecord.description &&
67+
Math.abs(record.timestamp - startupRecord.timestamp) <= 5_000,
68+
);
69+
if (localRecord) {
70+
localRecord.detail += `\n\n${startupRecord.detail}`;
71+
localRecord.frames.push(...startupRecord.frames);
72+
} else {
73+
records.push(startupRecord);
74+
}
75+
}
76+
return records.sort((left, right) => right.timestamp - left.timestamp);
6477
}
6578

6679
function compactFrame(line: string): string {
@@ -76,7 +89,7 @@ export function formatStartupCrashReport(
7689
app: { readonly version: string; readonly build: string },
7790
): string {
7891
const header = `T3 Code ${app.version} (${app.build})`;
79-
if (records.length === 0) return `${header}\nNo startup crashes recorded.`;
92+
if (records.length === 0) return `${header}\nNo crashes recorded.`;
8093
return [
8194
header,
8295
...records.map(

‎apps/mobile/src/lib/crashLog.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,27 @@ function persistCrashRecord(error: unknown): void {
4646
const record = {
4747
capturedAt: new Date().toISOString(),
4848
isFatal: true,
49-
message: cause?.message ?? safeErrorMessage(error),
50-
name: cause?.name ?? null,
51-
stack: cause?.stack ?? null,
49+
message: typeof cause?.message === "string" ? cause.message : safeErrorMessage(error),
50+
name: typeof cause?.name === "string" ? cause.name : null,
51+
stack: typeof cause?.stack === "string" ? cause.stack : null,
5252
};
5353
crashSequence += 1;
5454
// Keep lexical directory ordering aligned with capture order for records
5555
// written within the same millisecond.
5656
const sequence = String(crashSequence).padStart(6, "0");
5757
const file = new File(directory, `crash-${Date.now()}-${sequence}.json`);
5858
file.create({ intermediates: true, overwrite: true });
59-
// ErrorUtils may terminate the runtime as soon as we delegate to its handler.
60-
file.writeSync(JSON.stringify(record));
59+
try {
60+
// ErrorUtils may terminate the runtime as soon as we delegate to its handler.
61+
file.writeSync(JSON.stringify(record));
62+
} catch (error) {
63+
try {
64+
file.delete();
65+
} catch {
66+
// Cleanup failure must not replace the write error.
67+
}
68+
throw error;
69+
}
6170
for (const oldFile of crashLogFiles().slice(MAX_PERSISTED_CRASH_LOGS)) {
6271
try {
6372
oldFile.delete();

0 commit comments

Comments
 (0)