Skip to content

Commit a858f95

Browse files
minor refactor
simplify the code that deletes the heartbeat file Signed-off-by: nkomonen-amazon <[email protected]>
1 parent 35eb271 commit a858f95

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

packages/core/src/shared/crashMonitoring.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,11 @@ export class FileSystemState {
457457

458458
// ------------------ Heartbeat methods ------------------
459459
public async sendHeartbeat() {
460+
const extId = this.extId
460461
try {
461462
const now = this.deps.now()
462463
const func = async () => {
463-
const filePath = this.makeStateFilePath(this.extId)
464+
const filePath = this.makeStateFilePath(extId)
464465

465466
await fs.writeFile(filePath, JSON.stringify({ ...this.ext, lastHeartbeat: now }, undefined, 4))
466467

@@ -480,14 +481,10 @@ export class FileSystemState {
480481
return funcWithTelemetryRun
481482
} catch (e) {
482483
// delete this ext from the state to avoid an incorrectly reported crash since we could not send a new heartbeat
483-
await withFailCtx('sendHeartbeatFailureCleanup', () => this.clearHeartbeat())
484+
await this.deleteHeartbeatFile(extId, 'sendHeartbeatFailureCleanup')
484485
throw e
485486
}
486487
}
487-
/** Clears this extentions heartbeat from the state */
488-
public async clearHeartbeat() {
489-
await this.deleteHeartbeatFile(this.extId)
490-
}
491488

492489
/**
493490
* Indicates that this extension instance has gracefully shutdown.
@@ -502,28 +499,22 @@ export class FileSystemState {
502499
public async indicateGracefulShutdown(): Promise<void> {
503500
// By removing the heartbeat entry, the crash checkers will not be able to find this entry anymore, making it
504501
// impossible to report on since the file system is the source of truth
505-
await withFailCtx('indicateGracefulShutdown', () =>
506-
nodeFs.rm(this.makeStateFilePath(this.extId), { force: true })
507-
)
502+
await this.deleteHeartbeatFile(this.extId, 'indicateGracefulShutdown')
508503
}
509504

510505
// ------------------ Checker Methods ------------------
511506

512507
public async handleCrashedExt(ext: ExtInstance, fn: () => void) {
513-
await withFailCtx('handleCrashedExt', async () => {
514-
await this.deleteHeartbeatFile(ext)
515-
fn()
516-
})
508+
await this.deleteHeartbeatFile(ext, 'handleCrashedExt')
509+
await withFailCtx('handleCrashedExt', async () => fn())
517510
}
518511

519-
private async deleteHeartbeatFile(ext: ExtInstanceId | ExtInstance) {
520-
// Retry file deletion to prevent incorrect crash reports. Common Windows errors seen in telemetry: EPERM/EBUSY.
521-
// See: https://github.com/aws/aws-toolkit-vscode/pull/5335
522-
await withRetries(() => withFailCtx('deleteStaleRunningFile', () => fs.delete(this.makeStateFilePath(ext))), {
523-
maxRetries: 6,
524-
delay: 100,
525-
backoff: 2,
526-
})
512+
private async deleteHeartbeatFile(ext: ExtInstanceId | ExtInstance, ctx: string) {
513+
// IMPORTANT: Must use NodeFs here since this is used during shutdown
514+
const func = () => nodeFs.rm(this.makeStateFilePath(ext), { force: true })
515+
const funcWithCtx = () => withFailCtx(ctx, func)
516+
const funcWithRetries = withRetries(funcWithCtx, { maxRetries: 6, delay: 100, backoff: 2 })
517+
await funcWithRetries
527518
}
528519

529520
// ------------------ State data ------------------

0 commit comments

Comments
 (0)