Skip to content

Commit a425c97

Browse files
fix(crash): crash monitoring dir not being cleaned up
During development the underlying implementation was changed to get the OS uptime. The initial implementation returned uptime in minutes, but the new one returned it in seconds and this was not accounted for. As a result the crash monitoring folder was not being cleaned up when expected on computer restart Signed-off-by: nkomonen-amazon <[email protected]>
1 parent 907754c commit a425c97

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

packages/core/src/shared/crashMonitoring.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,11 @@ export class FileSystemState {
492492
return p
493493
}
494494
public async clearState(): Promise<void> {
495-
await withFailCtx('clearState', async () => fs.delete(this.stateDirPath, { force: true }))
495+
this.deps.devLogger?.debug('crashMonitoring: CLEAR_STATE: Started')
496+
await withFailCtx('clearState', async () => {
497+
await fs.delete(this.stateDirPath, { force: true, recursive: true })
498+
this.deps.devLogger?.debug('crashMonitoring: CLEAR_STATE: Succeeded')
499+
})
496500
}
497501
public async getAllExts(): Promise<ExtInstanceHeartbeat[]> {
498502
const res = await withFailCtx('getAllExts', async () => {

packages/core/src/shared/utilities/osUtils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@ import * as os from 'os'
1515
* Use this function to perform one-time initialization tasks that should only happen
1616
* once per OS session, regardless of how many extension instances are running.
1717
*/
18-
export async function isNewOsSession(now = () => globals.clock.Date.now(), uptime = () => os.uptime()) {
18+
export async function isNewOsSession(now = () => globals.clock.Date.now(), uptimeMillis = () => os.uptime() * 1000) {
1919
// Windows does not have an ephemeral /tmp/ folder that deletes on shutdown, while unix-like os's do.
2020
// So in Windows we calculate the start time and see if it changed from the previous known start time.
2121
const lastStartTime = globals.globalState.tryGet('lastOsStartTime', Number)
22-
// uptime() returns seconds, convert to ms
23-
const currentOsStartTime = now() - uptime() * 1000 * 60
22+
23+
const uptime = uptimeMillis()
24+
const currentOsStartTime = now() - uptime
2425

2526
if (lastStartTime === undefined) {
2627
await globals.globalState.update('lastOsStartTime', currentOsStartTime)
2728
return true
2829
}
2930

3031
// If the current start time is later than the last, it means we are in a new session since they should be the same value.
31-
// But to account for small differences in how the current time is calculate, we add in a 5 second buffer.
32+
// But to account for minor millisecond differnces, we add in a 5 second buffer.
3233
if (currentOsStartTime - 1000 * 5 > lastStartTime) {
3334
await globals.globalState.update('lastOsStartTime', currentOsStartTime)
3435
return true

packages/core/src/test/shared/utilities/osUtils.test.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,32 @@ describe('isNewOsSession', () => {
2222
})
2323

2424
it('unix-like: returns true when expected', async () => {
25-
const uptimeStub = sandbox.stub()
25+
const uptimeMillisStub = sandbox.stub()
2626
const now = sandbox.stub()
27-
// We started our computer at 2 minutes since epoch (time - pc uptime)
28-
// and the comptuer has been on for 1 minute. So the OS started 1 minute since epoch.
29-
now.returns(60_000 + 60_000)
30-
uptimeStub.returns(1)
27+
// We started our computer at 1 minutes since epoch and the comptuer uptime has been 1 minute.
28+
// So the OS started at the epoch (time - uptime).
29+
now.returns(0) // the epoch time
30+
uptimeMillisStub.returns(0) // this session has just started
3131

3232
// On a brand new session the first caller will get true
33-
assert.strictEqual(await isNewOsSession(now, uptimeStub), true)
33+
assert.strictEqual(await isNewOsSession(now, uptimeMillisStub), true)
3434
// Subsequent callers will get false
35-
assert.strictEqual(await isNewOsSession(now, uptimeStub), false)
36-
37-
// Start a computer session 10 minutes from epoch
38-
uptimeStub.returns(0)
39-
now.returns(60_000 * 10)
40-
assert.strictEqual(await isNewOsSession(now, uptimeStub), true)
41-
// Anything that is within a 5 second threshold of the last session time, is considered the same session
42-
now.returns(60_000 * 10 + 5000)
43-
assert.strictEqual(await isNewOsSession(now, uptimeStub), false)
44-
now.returns(60_000 * 10 + 5000 + 1)
45-
assert.strictEqual(await isNewOsSession(now, uptimeStub), true)
46-
47-
// A non-zero uptime
48-
uptimeStub.returns(5) // The computer has been running for 5 minutes already, so the start time is relative to this.
49-
now.returns(60_000 * 10 + 5000 + 60_000 * 10) // 5 minutes since last session
50-
// Nothing changes since the diff between uptime and the last start has not changed
51-
assert.strictEqual(await isNewOsSession(now, uptimeStub), true)
35+
assert.strictEqual(await isNewOsSession(now, uptimeMillisStub), false)
36+
37+
// 10 minutes later, same session
38+
now.returns(1000 * 60 * 10)
39+
uptimeMillisStub.returns(1000 * 60 * 10) // This scales proportionately with the current time
40+
// This is still the same session, so we get false
41+
assert.strictEqual(await isNewOsSession(now, uptimeMillisStub), false)
42+
43+
// Test the lowerbound of what is considered a new session
44+
// Pretend we started a new computer session 5 seconds after the initial session
45+
uptimeMillisStub.returns(0)
46+
now.returns(5000)
47+
// Anything that is within a 5 second threshold of the last session time, is considered the SAME session
48+
assert.strictEqual(await isNewOsSession(now, uptimeMillisStub), false)
49+
// This is 1 millisecond after the threshold, it is considered a NEW session
50+
now.returns(5000 + 1)
51+
assert.strictEqual(await isNewOsSession(now, uptimeMillisStub), true)
5252
})
5353
})

0 commit comments

Comments
 (0)