Skip to content

Commit 0f27710

Browse files
committed
fix: Electron IPC serializer issue on certain logs
Now manually JSON.stringify logs from the renderer thread before sending them to the main thread
1 parent fb0263a commit 0f27710

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

apps/ledger-live-desktop/src/main/setup.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,21 @@ ipcMain.on("updater", (e, type) => {
3333
*/
3434
ipcMain.handle(
3535
"save-logs",
36-
async (_event, path: Electron.SaveDialogReturnValue, rendererLogs: unknown[]) => {
36+
async (_event, path: Electron.SaveDialogReturnValue, rendererLogsStr: string) => {
3737
if (!path.canceled && path.filePath) {
3838
const inMemoryLogger = InMemoryLogger.getLogger();
3939
const internalLogs = inMemoryLogger.getLogs();
40+
41+
// The deserialization would have been done internally by electron if `rendererLogs` was passed directly as a JS object/array.
42+
// But it avoids certain issues with the serialization/deserialization done by electron.
43+
let rendererLogs: unknown[] = [];
44+
try {
45+
rendererLogs = JSON.parse(rendererLogsStr) as unknown[];
46+
} catch (error) {
47+
console.error("Error while parsing logs from the renderer thread", error);
48+
return;
49+
}
50+
4051
console.log(
4152
`Saving ${rendererLogs.length} logs from the renderer thread and ${internalLogs.length} logs from the internal thread`,
4253
);

apps/ledger-live-desktop/src/renderer/components/ExportLogsButton.tsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@ import Button, { Props as ButtonProps } from "~/renderer/components/Button";
1313
import { accountsSelector } from "~/renderer/reducers/accounts";
1414

1515
const saveLogs = async (path: Electron.SaveDialogReturnValue) => {
16-
// Requests the main thread to save logs in a file
17-
await ipcRenderer.invoke("save-logs", path, memoryLogger.getMemoryLogs());
16+
const memoryLogs = memoryLogger.getMemoryLogs();
17+
18+
try {
19+
// Serializes ourself with `stringify` to avoid "object could not be cloned" errors from the electron IPC serializer.
20+
const memoryLogsStr = JSON.stringify(memoryLogs, null, 2);
21+
22+
// Requests the main thread to save logs in a file
23+
await ipcRenderer.invoke("save-logs", path, memoryLogsStr);
24+
} catch (error) {
25+
console.error("Error while requesting to save logs from the renderer thread", error);
26+
}
1827
};
1928

2029
type RestProps = ButtonProps & {

0 commit comments

Comments
 (0)