77 Scope ,
88 ScopeData ,
99 SentryError ,
10+ Session ,
1011} from '@sentry/core' ;
1112import { NodeClient } from '@sentry/node' ;
1213import { app , crashReporter } from 'electron' ;
@@ -16,7 +17,7 @@ import { getEventDefaults } from '../../context';
1617import { EXIT_REASONS , getSentryCachePath , usesCrashpad } from '../../electron-normalize' ;
1718import { getRendererProperties , trackRendererProperties } from '../../renderers' ;
1819import { ElectronMainOptions } from '../../sdk' ;
19- import { checkPreviousSession , sessionCrashed } from '../../sessions' ;
20+ import { previousSessionWasAbnormal , restorePreviousSession , setPreviousSessionAsCurrent } from '../../sessions' ;
2021import { BufferedWriteStore } from '../../store' ;
2122import { getMinidumpLoader , MinidumpLoader } from './minidump-loader' ;
2223
@@ -82,7 +83,7 @@ export const sentryMinidumpIntegration = defineIntegration((options: Options = {
8283
8384 async function sendNativeCrashes (
8485 client : NodeClient ,
85- getEvent : ( minidumpProcess : string | undefined ) => Event ,
86+ getEvent : ( minidumpProcess : string | undefined ) => Event | Promise < Event > ,
8687 ) : Promise < boolean > {
8788 // Whenever we are called, assume that the crashes we are going to load down
8889 // below have occurred recently. This means, we can use the same event data
@@ -108,29 +109,11 @@ export const sentryMinidumpIntegration = defineIntegration((options: Options = {
108109 await minidumpLoader ?.( deleteAll , async ( minidumpProcess , attachment ) => {
109110 minidumpFound = true ;
110111
111- const event = getEvent ( minidumpProcess ) ;
112-
113- // If this is a native main process crash, we need to apply the scope and context from the previous run
114- if ( event . tags ?. [ 'event.process' ] === 'browser' ) {
115- const previousRun = await scopeLastRun ;
116- if ( previousRun ) {
117- if ( previousRun . scope ) {
118- applyScopeDataToEvent ( event , previousRun . scope ) ;
119- }
120-
121- event . release = previousRun . event ?. release || event . release ;
122- event . environment = previousRun . event ?. environment || event . environment ;
123- event . contexts = previousRun . event ?. contexts || event . contexts ;
124- }
125- }
126-
127- if ( ! event ) {
128- return ;
129- }
112+ const event = await getEvent ( minidumpProcess ) ;
130113
131114 if ( minidumpsRemaining > 0 ) {
132115 minidumpsRemaining -= 1 ;
133- captureEvent ( event as Event , { attachments : [ attachment ] } ) ;
116+ captureEvent ( event , { attachments : [ attachment ] } ) ;
134117 }
135118 } ) ;
136119
@@ -145,7 +128,7 @@ export const sentryMinidumpIntegration = defineIntegration((options: Options = {
145128 ) : Promise < void > {
146129 const { getRendererName } = options ;
147130
148- const found = await sendNativeCrashes ( client , ( minidumpProcess ) => {
131+ await sendNativeCrashes ( client , ( minidumpProcess ) => {
149132 // We only call 'getRendererName' if this was in fact a renderer crash
150133 const crashedProcess =
151134 ( minidumpProcess === 'renderer' && getRendererName ? getRendererName ( contents ) : minidumpProcess ) ||
@@ -170,20 +153,12 @@ export const sentryMinidumpIntegration = defineIntegration((options: Options = {
170153 } ,
171154 } ;
172155 } ) ;
173-
174- if ( found ) {
175- sessionCrashed ( ) ;
176- }
177156 }
178157
179- async function sendChildProcessCrash (
180- client : NodeClient ,
181- options : ElectronMainOptions ,
182- details : Omit < Electron . Details , 'exitCode' > ,
183- ) : Promise < void > {
158+ async function sendChildProcessCrash ( client : NodeClient , details : Omit < Electron . Details , 'exitCode' > ) : Promise < void > {
184159 logger . log ( `${ details . type } process has ${ details . reason } ` ) ;
185160
186- const found = await sendNativeCrashes ( client , ( minidumpProcess ) => ( {
161+ await sendNativeCrashes ( client , ( minidumpProcess ) => ( {
187162 contexts : {
188163 electron : { details } ,
189164 } ,
@@ -197,10 +172,6 @@ export const sentryMinidumpIntegration = defineIntegration((options: Options = {
197172 event_type : 'native' ,
198173 } ,
199174 } ) ) ;
200-
201- if ( found ) {
202- sessionCrashed ( ) ;
203- }
204175 }
205176
206177 return {
@@ -238,25 +209,47 @@ export const sentryMinidumpIntegration = defineIntegration((options: Options = {
238209 } ) ;
239210 app . on ( 'child-process-gone' , async ( _ , details ) => {
240211 if ( EXIT_REASONS . includes ( details . reason ) ) {
241- await sendChildProcessCrash ( client , options , details ) ;
212+ await sendChildProcessCrash ( client , details ) ;
242213 }
243214 } ) ;
244215
216+ let sessionToRestore : Session | undefined ;
217+
245218 // Start to submit recent minidump crashes. This will load breadcrumbs and
246219 // context information that was cached on disk in the previous app run, prior to the crash.
247- sendNativeCrashes ( client , ( minidumpProcess ) => ( {
248- level : 'fatal' ,
249- platform : 'native' ,
250- tags : {
251- 'event.environment' : 'native' ,
252- 'event.process' : minidumpProcess || ( usesCrashpad ( ) ? 'unknown' : 'browser' ) ,
253- } ,
254- } ) )
255- . then ( ( minidumpsFound ) =>
256- // Check for previous uncompleted session. If a previous session exists
257- // and no minidumps were found, its likely an abnormal exit
258- checkPreviousSession ( minidumpsFound ) ,
259- )
220+ sendNativeCrashes ( client , async ( minidumpProcess ) => {
221+ const event : Event = {
222+ level : 'fatal' ,
223+ platform : 'native' ,
224+ tags : {
225+ 'event.environment' : 'native' ,
226+ 'event.process' : minidumpProcess || ( usesCrashpad ( ) ? 'unknown' : 'browser' ) ,
227+ } ,
228+ } ;
229+
230+ // This crash was found at startup, we need to apply the scope and context from the previous run
231+ const previousRun = await scopeLastRun ;
232+ if ( previousRun ) {
233+ if ( previousRun . scope ) {
234+ applyScopeDataToEvent ( event , previousRun . scope ) ;
235+ }
236+
237+ event . release = previousRun . event ?. release ;
238+ event . environment = previousRun . event ?. environment ;
239+ event . contexts = previousRun . event ?. contexts ;
240+ }
241+
242+ sessionToRestore = await setPreviousSessionAsCurrent ( ) ;
243+
244+ return event ;
245+ } )
246+ . then ( async ( minidumpsFound ) => {
247+ if ( ! minidumpsFound ) {
248+ await previousSessionWasAbnormal ( ) ;
249+ } else if ( sessionToRestore ) {
250+ restorePreviousSession ( sessionToRestore ) ;
251+ }
252+ } )
260253 . catch ( ( error ) => logger . error ( error ) ) ;
261254 } ,
262255 } ;
0 commit comments