@@ -2,7 +2,7 @@ import * as fs from 'node:fs/promises'
22import * as path from 'node:path'
33
44import type { Observable } from '@datadog/browser-core'
5- import { monitor , ErrorHandling , generateUUID } from '@datadog/browser-core'
5+ import { monitorError , monitor , ErrorHandling , generateUUID , setTimeout , ONE_SECOND } from '@datadog/browser-core'
66import { RumEventType } from '@datadog/browser-rum-core'
77import { app , crashReporter } from 'electron'
88
@@ -169,72 +169,77 @@ export function startCrashMonitoring(onRumEventObservable: Observable<CollectedR
169169 } )
170170
171171 // Wait for app to be ready before accessing crash dumps directory
172- void app . whenReady ( ) . then (
173- monitor ( async ( ) => {
174- const crashesDirectory = app . getPath ( 'crashDumps' )
175-
176- const crashContextPath = path . join ( crashesDirectory , CRASH_CONTEXT_FILE_NAME )
177-
178- // Check if crash context file exists
179- try {
180- await fs . access ( crashContextPath )
181- } catch {
182- console . warn ( '[Datadog] No crash context found' )
183- // Stop reporting, we don't want to report incorrect data
184- ready = true
185- callbacks . forEach ( ( callback ) => callback ( ) )
186- return
187- }
188-
189- // Read crash context from previous session
190- const crashContext = await fs . readFile ( crashContextPath , 'utf-8' )
191- const crashContextData = JSON . parse ( crashContext ) as CrashContext
192-
193- // Check if there are any crash reports pending
194- const pendingCrashReports = await getFilesRecursive ( crashesDirectory , '.dmp' )
195-
196- if ( pendingCrashReports . length === 0 ) {
197- console . log ( '[Datadog] No pending crash reports found' )
198- } else {
199- console . log ( `[Datadog] ${ pendingCrashReports . length } pending crash reports found` )
200- }
201-
202- // Process crash reports in parallel
203- await Promise . all (
204- pendingCrashReports . map ( async ( reportPath ) => {
205- const reportMetadata = await fs . stat ( reportPath )
206- const reportBytes = await fs . readFile ( reportPath )
207-
208- const resultJson = await process_minidump_with_stackwalk ( reportBytes )
209- const minidumpResult : MinidumpResult = JSON . parse ( resultJson )
210-
211- const crashTime = new Date ( reportMetadata . ctime ) . getTime ( )
212-
213- const reportName = path . basename ( reportPath )
214- const rumErrorEvent = createCrashErrorEvent (
215- minidumpResult ,
216- reportName ,
217- crashTime ,
218- applicationId ,
219- crashContextData . sessionId ,
220- crashContextData . viewId
221- )
222-
223- onRumEventObservable . notify ( {
224- event : rumErrorEvent ,
225- source : 'main-process' ,
226- } )
227-
228- // delete the crash report
229- await fs . unlink ( reportPath )
230- console . log ( `[Datadog] crash processed: ${ reportName } ` )
231- } )
172+ void app . whenReady ( ) . then ( ( ) =>
173+ // wait a bit more to prevent crash on windows ¯\_(ツ)_/¯
174+ setTimeout ( ( ) => {
175+ processCrashesFiles ( onRumEventObservable , applicationId ) . catch ( monitorError )
176+ } , ONE_SECOND )
177+ )
178+ }
179+
180+ async function processCrashesFiles ( onRumEventObservable : Observable < CollectedRumEvent > , applicationId : string ) {
181+ const crashesDirectory = app . getPath ( 'crashDumps' )
182+
183+ const crashContextPath = path . join ( crashesDirectory , CRASH_CONTEXT_FILE_NAME )
184+
185+ // Check if crash context file exists
186+ try {
187+ await fs . access ( crashContextPath )
188+ } catch {
189+ console . warn ( '[Datadog] No crash context found' )
190+ // Stop reporting, we don't want to report incorrect data
191+ ready = true
192+ callbacks . forEach ( ( callback ) => callback ( ) )
193+ return
194+ }
195+
196+ // Read crash context from previous session
197+ const crashContext = await fs . readFile ( crashContextPath , 'utf-8' )
198+ const crashContextData = JSON . parse ( crashContext ) as CrashContext
199+
200+ // Check if there are any crash reports pending
201+ const pendingCrashReports = await getFilesRecursive ( crashesDirectory , '.dmp' )
202+
203+ if ( pendingCrashReports . length === 0 ) {
204+ console . log ( '[Datadog] No pending crash reports found' )
205+ } else {
206+ console . log ( `[Datadog] ${ pendingCrashReports . length } pending crash reports found` )
207+ }
208+
209+ // Process crash reports in parallel
210+ await Promise . all (
211+ pendingCrashReports . map ( async ( reportPath ) => {
212+ const reportMetadata = await fs . stat ( reportPath )
213+ const reportBytes = await fs . readFile ( reportPath )
214+
215+ const resultJson = await process_minidump_with_stackwalk ( reportBytes )
216+ const minidumpResult : MinidumpResult = JSON . parse ( resultJson )
217+
218+ const crashTime = new Date ( reportMetadata . ctime ) . getTime ( )
219+
220+ const reportName = path . basename ( reportPath )
221+ const rumErrorEvent = createCrashErrorEvent (
222+ minidumpResult ,
223+ reportName ,
224+ crashTime ,
225+ applicationId ,
226+ crashContextData . sessionId ,
227+ crashContextData . viewId
232228 )
233229
234- ready = true
235- callbacks . forEach ( ( callback ) => callback ( ) )
230+ onRumEventObservable . notify ( {
231+ event : rumErrorEvent ,
232+ source : 'main-process' ,
233+ } )
234+
235+ // delete the crash report
236+ await fs . unlink ( reportPath )
237+ console . log ( `[Datadog] crash processed: ${ reportName } ` )
236238 } )
237239 )
240+
241+ ready = true
242+ callbacks . forEach ( ( callback ) => callback ( ) )
238243}
239244
240245async function getFilesRecursive ( dir : string , ext : string ) {
0 commit comments