Skip to content

Commit 35eb271

Browse files
refactor: remove PID requirement
We do not need the PID anymore since we fixed the sessionId so that a new one is generated even after the ext host crashes and restarts. The PID previously allowed us to differentiate between ide instances with the same sessionId but different PIDs Signed-off-by: nkomonen-amazon <[email protected]>
1 parent 340418c commit 35eb271

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

packages/core/src/shared/crashMonitoring.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { getLogger } from './logger/logger'
1818
import { crashMonitoringDirName } from './constants'
1919
import { throwOnUnstableFileSystem } from './filesystemUtilities'
2020
import { withRetries } from './utilities/functionUtils'
21+
import { truncateUuid } from './crypto'
2122

2223
const className = 'CrashMonitoring'
2324

@@ -307,17 +308,15 @@ class CrashChecker {
307308
timestamp: ext.lastHeartbeat,
308309
})
309310

310-
devLogger?.debug(
311-
`crashMonitoring: CRASH: following has crashed: pid ${ext.extHostPid} + sessionId: ${ext.sessionId}`
312-
)
311+
devLogger?.debug(`crashMonitoring: CRASH: following has crashed: sessionId: ${ext.sessionId}`)
313312
})
314313
}
315314

316315
if (isDevMode) {
317-
const before = knownExts.map((i) => i.extHostPid)
318-
const after = runningExts.map((i) => i.extHostPid)
316+
const before = knownExts.map((i) => truncateUuid(i.sessionId))
317+
const after = runningExts.map((i) => truncateUuid(i.sessionId))
319318
// Sanity check: ENSURE THAT AFTER === ACTUAL or this implies that our data is out of sync
320-
const afterActual = (await state.getAllExts()).map((i) => i.extHostPid)
319+
const afterActual = (await state.getAllExts()).map((i) => truncateUuid(i.sessionId))
321320
devLogger?.debug(
322321
`crashMonitoring: CHECKED: Result of cleaning up stopped instances\nBEFORE: ${JSON.stringify(before)}\nAFTER: ${JSON.stringify(after)}\nACTUAL: ${JSON.stringify(afterActual)}`
323322
)
@@ -386,7 +385,6 @@ class TimeLag {
386385
*/
387386
type MementoStateDependencies = {
388387
memento: vscode.Memento
389-
pid: number
390388
sessionId: string
391389
workDirPath: string
392390
isDevMode: boolean
@@ -401,7 +399,6 @@ function getDefaultDependencies(): MementoStateDependencies {
401399
workDirPath: path.join(globals.context.globalStorageUri.fsPath),
402400
memento: globals.globalState as vscode.Memento,
403401
isStateStale: () => isNewOsSession(),
404-
pid: process.pid,
405402
sessionId: _getSessionId(),
406403
isDevMode: getIsDevMode(),
407404
devLogger: getIsDevMode() ? getLogger() : undefined,
@@ -433,7 +430,6 @@ export class FileSystemState {
433430
constructor(protected readonly deps: MementoStateDependencies) {
434431
this.stateDirPath = path.join(this.deps.workDirPath, crashMonitoringDirName)
435432

436-
this.deps.devLogger?.debug(`crashMonitoring: pid: ${this.deps.pid}`)
437433
this.deps.devLogger?.debug(`crashMonitoring: sessionId: ${this.deps.sessionId.slice(0, 8)}-...`)
438434
this.deps.devLogger?.debug(`crashMonitoring: dir: ${this.stateDirPath}`)
439435
}
@@ -538,7 +534,6 @@ export class FileSystemState {
538534
protected get ext(): ExtInstance {
539535
return {
540536
sessionId: this.deps.sessionId,
541-
extHostPid: this.deps.pid,
542537
isDebug: isDebugInstance() ? true : undefined,
543538
}
544539
}
@@ -550,9 +545,9 @@ export class FileSystemState {
550545
* - The Extension Host PID used in addition to the session ID should be good enough to uniquely identiy.
551546
*/
552547
protected createExtId(ext: ExtInstance): ExtInstanceId {
553-
return `${ext.extHostPid}_${ext.sessionId}`
548+
return ext.sessionId
554549
}
555-
private readonly fileSuffix = 'running'
550+
private readonly fileSuffix = 'v1'
556551
private makeStateFilePath(ext: ExtInstance | ExtInstanceId) {
557552
const extId = typeof ext === 'string' ? ext : this.createExtId(ext)
558553
return path.join(this.stateDirPath, extId + `.${this.fileSuffix}`)
@@ -654,7 +649,6 @@ type ExtInstanceId = string
654649

655650
/** The static metadata of an instance of this extension */
656651
export type ExtInstance = {
657-
extHostPid: number
658652
sessionId: string
659653
lastHeartbeat?: number
660654
/**

packages/core/src/shared/crypto.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,15 @@ export function isUuid(uuid: string): boolean {
4242
const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
4343
return uuidPattern.test(uuid)
4444
}
45+
46+
/**
47+
* Eg: 'aaaabbbb-cccc-dddd-eeee-ffffhhhhiiii' -> 'aaaa...iiii'
48+
*/
49+
export function truncateUuid(uuid: string) {
50+
if (uuid.length !== 36) {
51+
throw new Error(`Cannot truncate uuid of value: "${uuid}"`)
52+
}
53+
54+
const cleanedUUID = uuid.replace(/-/g, '')
55+
return `${cleanedUUID.substring(0, 4)}...${cleanedUUID.substring(cleanedUUID.length - 4)}`
56+
}

packages/core/src/test/shared/crashMonitoring.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { CrashMonitoring, ExtInstance, crashMonitoringStateFactory } from '../..
1010
import { isCI } from '../../shared/vscode/env'
1111
import { getLogger } from '../../shared/logger/logger'
1212
import { SinonSandbox, createSandbox } from 'sinon'
13-
import { fs } from '../../shared'
13+
import { fs, randomUUID } from '../../shared'
1414
import path from 'path'
1515

1616
class TestCrashMonitoring extends CrashMonitoring {
@@ -55,13 +55,11 @@ export const crashMonitoringTest = async () => {
5555

5656
async function makeTestExtension(id: number, opts?: { isStateStale: () => Promise<boolean> }) {
5757
const isStateStale = opts?.isStateStale ?? (() => Promise.resolve(false))
58-
const sessionId = `sessionId-${id}`
59-
const pid = Number(String(id).repeat(6))
58+
const sessionId = randomUUID()
6059

6160
const state = await crashMonitoringStateFactory({
6261
workDirPath: testFolder.path,
6362
isStateStale,
64-
pid,
6563
sessionId: sessionId,
6664
now: () => globals.clock.Date.now(),
6765
memento: globals.globalState,
@@ -71,7 +69,6 @@ export const crashMonitoringTest = async () => {
7169
const ext = new TestCrashMonitoring(state, checkInterval, true, false, getLogger())
7270
spawnedExtensions.push(ext)
7371
const metadata = {
74-
extHostPid: pid,
7572
sessionId,
7673
lastHeartbeat: globals.clock.Date.now(),
7774
isDebug: undefined,
@@ -276,8 +273,7 @@ export const crashMonitoringTest = async () => {
276273
const state = await crashMonitoringStateFactory({
277274
workDirPath: testFolder.path,
278275
isStateStale: () => Promise.resolve(false),
279-
pid: 1111,
280-
sessionId: 'sessionId_1111',
276+
sessionId: randomUUID(),
281277
now: () => globals.clock.Date.now(),
282278
memento: globals.globalState,
283279
isDevMode: true,

packages/core/src/test/shared/crypto.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import assert from 'assert'
7-
import { isUuid, randomUUID } from '../../shared/crypto'
7+
import { isUuid, randomUUID, truncateUuid } from '../../shared/crypto'
88

99
describe('crypto', function () {
1010
describe('randomUUID()', function () {
@@ -44,3 +44,23 @@ describe('crypto', function () {
4444
assert.equal(isUuid('47fe01cf-f37a-4e7c-b971-d10fe5897763'.toUpperCase()), true)
4545
})
4646
})
47+
48+
describe('truncateUUID', function () {
49+
it('should return the first 4 and last 4 characters of a valid UUID', function () {
50+
const fullUUID = 'aaaabbbb-cccc-dddd-eeee-ffffhhhhiiii'
51+
const result = truncateUuid(fullUUID)
52+
assert.strictEqual(result, 'aaaa...iiii')
53+
})
54+
55+
it('should handle a different valid UUID correctly', function () {
56+
const fullUUID = '12340000-0000-0000-0000-000000005678'
57+
const result = truncateUuid(fullUUID)
58+
assert.strictEqual(result, '1234...5678')
59+
})
60+
61+
it('should throw an error if the input is not 36 characters long', function () {
62+
assert.throws(() => {
63+
truncateUuid('invalid-uuid')
64+
}, /Cannot truncate uuid of value: "invalid-uuid"/)
65+
})
66+
})

0 commit comments

Comments
 (0)