Skip to content

Commit

Permalink
fix: Don't call app.getPath('userData') before SDK init (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish authored Dec 5, 2023
1 parent aca8dd4 commit e84a302
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## 4.15.1

- fix: Don't call `app.getPath('userData')` before SDK `init` (#797)

## 4.15.0

- feat: Update from [v7.74.0](https://github.com/getsentry/sentry-javascript/releases/tag/7.74.0) to
Expand Down
5 changes: 4 additions & 1 deletion src/main/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { mkdir, readdir, readFile, stat, statSync, unlink, writeFile } from 'fs'
import { dirname, join, resolve } from 'path';
import { promisify } from 'util';

export const sentryCachePath = join(app ? app.getPath('userData') : '', 'sentry');
/** Gets the Sentry Cache path */
export function getSentryCachePath(): string {
return join(app.getPath('userData'), 'sentry');
}

export const writeFileAsync = promisify(writeFile);
export const readFileAsync = promisify(readFile);
Expand Down
4 changes: 2 additions & 2 deletions src/main/integrations/sentry-minidump/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { app, crashReporter } from 'electron';
import { mergeEvents } from '../../../common';
import { getDefaultEnvironment, getDefaultReleaseName, getEventDefaults } from '../../context';
import { EXIT_REASONS, onChildProcessGone, onRendererProcessGone } from '../../electron-normalize';
import { sentryCachePath } from '../../fs';
import { getSentryCachePath } from '../../fs';
import { getRendererProperties, trackRendererProperties } from '../../renderers';
import { ElectronMainOptions } from '../../sdk';
import { checkPreviousSession, sessionCrashed } from '../../sessions';
Expand Down Expand Up @@ -50,7 +50,7 @@ export class SentryMinidump implements Integration {

this._startCrashReporter();

this._scopeStore = new BufferedWriteStore<PreviousRun>(sentryCachePath, 'scope_v3', {
this._scopeStore = new BufferedWriteStore<PreviousRun>(getSentryCachePath(), 'scope_v3', {
scope: new Scope(),
});

Expand Down
24 changes: 16 additions & 8 deletions src/main/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ import { logger } from '@sentry/utils';
import { app } from 'electron';

import { isAnrChildProcess } from './anr';
import { sentryCachePath } from './fs';
import { getSentryCachePath } from './fs';
import { Store } from './store';

const PERSIST_INTERVAL_MS = 60_000;

/** Stores the app session in case of termination due to main process crash or app killed */
const sessionStore = new Store<SessionContext | undefined>(sentryCachePath, 'session', undefined);

let sessionStore: Store<SessionContext | undefined> | undefined;
/** Previous session if it did not exit cleanly */
let previousSession: Promise<Partial<Session> | undefined> | undefined = sessionStore.get();
let previousSession: Promise<Partial<Session> | undefined> | undefined;

function getSessionStore(): Store<SessionContext | undefined> {
if (!sessionStore) {
sessionStore = new Store<SessionContext | undefined>(getSentryCachePath(), 'session', undefined);
previousSession = sessionStore.get();
}

return sessionStore;
}

let persistTimer: NodeJS.Timer | undefined;

Expand All @@ -27,14 +35,14 @@ export async function startSession(sendOnCreate: boolean): Promise<void> {
hub.captureSession();
}

await sessionStore.set(session);
await getSessionStore().set(session);

// Every PERSIST_INTERVAL, write the session to disk
persistTimer = setInterval(async () => {
const currentSession = hub.getScope()?.getSession();
// Only bother saving if it hasn't already ended
if (currentSession && currentSession.status === 'ok') {
await sessionStore.set(currentSession);
await getSessionStore().set(currentSession);
}
}, PERSIST_INTERVAL_MS);
}
Expand All @@ -60,7 +68,7 @@ export async function endSession(): Promise<void> {
logger.log('No session');
}

await sessionStore.clear();
await getSessionStore().clear();

await flush(2_000);
}
Expand All @@ -71,7 +79,7 @@ export async function unreportedDuringLastSession(crashDate: Date | undefined):
return false;
}

const previousSessionModified = await sessionStore.getModifiedDate();
const previousSessionModified = await getSessionStore().getModifiedDate();
// There is no previous session
if (previousSessionModified == undefined) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/main/transports/electron-offline-net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { logger } from '@sentry/utils';
import { net } from 'electron';
import { join } from 'path';

import { sentryCachePath } from '../fs';
import { getSentryCachePath } from '../fs';
import { createElectronNetRequestExecutor, ElectronNetTransportOptions } from './electron-net';
import { PersistedRequestQueue } from './queue';

Expand Down Expand Up @@ -59,7 +59,7 @@ function isRateLimited(result: TransportMakeRequestResponse): boolean {
export function makeElectronOfflineTransport(options: ElectronOfflineTransportOptions): Transport {
const netMakeRequest = createElectronNetRequestExecutor(options.url, options.headers || {});
const queue: PersistedRequestQueue = new PersistedRequestQueue(
join(sentryCachePath, 'queue'),
join(getSentryCachePath(), 'queue'),
options.maxQueueAgeDays,
options.maxQueueCount,
);
Expand Down
13 changes: 13 additions & 0 deletions test/unit/getPath-test-app/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { app } = require('electron');

process.on('uncaughtException', () => {
app.exit(1);
});

app.getPath = () => {
app.exit(1);
};

require('../../../main');

app.exit(0);
9 changes: 9 additions & 0 deletions test/unit/getPath-test-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "electron-test",
"version": "1.0.0",
"main": "main.js",
"scripts": {

"start": "../../../node_modules/.bin/electron ."
}
}
13 changes: 13 additions & 0 deletions test/unit/getPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect } from 'chai';
import { spawnSync } from 'child_process';
import { join } from 'path';

describe('app.getPath', () => {
it('not called before init', () => {
const result = spawnSync('yarn', ['start'], { cwd: join(__dirname, 'getPath-test-app') });
// status is null on Windows in CI for some unknown reason
if (process.platform !== 'win32') {
expect(result.status).to.equal(0);
}
});
});

0 comments on commit e84a302

Please sign in to comment.