Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getMachineId should not use errors.onUnexpectedError #176512

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/vs/base/node/id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/

import { networkInterfaces } from 'os';
import * as errors from 'vs/base/common/errors';
import { TernarySearchTree } from 'vs/base/common/ternarySearchTree';
import * as uuid from 'vs/base/common/uuid';
import { getMac } from 'vs/base/node/macAddress';
Expand Down Expand Up @@ -78,10 +77,10 @@ export const virtualMachineHint: { value(): number } = new class {
};

let machineId: Promise<string>;
export async function getMachineId(): Promise<string> {
export async function getMachineId(errorLogger: (error: any) => void): Promise<string> {
if (!machineId) {
machineId = (async () => {
const id = await getMacMachineId();
const id = await getMacMachineId(errorLogger);

return id || uuid.generateUuid(); // fallback, generate a UUID
})();
Expand All @@ -90,13 +89,13 @@ export async function getMachineId(): Promise<string> {
return machineId;
}

async function getMacMachineId(): Promise<string | undefined> {
async function getMacMachineId(errorLogger: (error: any) => void): Promise<string | undefined> {
try {
const crypto = await import('crypto');
const macAddress = getMac();
return crypto.createHash('sha256').update(macAddress, 'utf8').digest('hex');
} catch (err) {
errors.onUnexpectedError(err);
errorLogger(err);
return undefined;
}
}
4 changes: 3 additions & 1 deletion src/vs/base/test/node/id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import { flakySuite } from 'vs/base/test/node/testUtils';
flakySuite('ID', () => {

test('getMachineId', async function () {
const id = await getMachineId();
const errors = [];
const id = await getMachineId(err => errors.push(err));
assert.ok(id);
assert.strictEqual(errors.length, 0);
});

test('getMac', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/code/electron-main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ export class CodeApplication extends Disposable {

// Resolve unique machine ID
this.logService.trace('Resolving machine identifier...');
const machineId = await resolveMachineId(this.stateService);
const machineId = await resolveMachineId(this.stateService, this.logService);
this.logService.trace(`Resolved machine identifier: ${machineId}`);

// Shared process
Expand Down
2 changes: 1 addition & 1 deletion src/vs/code/node/cliProcessMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class CliMain extends Disposable {
commonProperties: (async () => {
let machineId: string | undefined = undefined;
try {
machineId = await resolveMachineId(stateService);
machineId = await resolveMachineId(stateService, logService);
} catch (error) {
if (error.code !== 'ENOENT') {
logService.error(error);
Expand Down
5 changes: 3 additions & 2 deletions src/vs/platform/telemetry/electron-main/telemetryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { ILogService } from 'vs/platform/log/common/log';
import { IStateService } from 'vs/platform/state/node/state';
import { machineIdKey } from 'vs/platform/telemetry/common/telemetry';
import { resolveMachineId as resolveNodeMachineId } from 'vs/platform/telemetry/node/telemetryUtils';

export async function resolveMachineId(stateService: IStateService) {
export async function resolveMachineId(stateService: IStateService, logService: ILogService) {
// Call the node layers implementation to avoid code duplication
const machineId = await resolveNodeMachineId(stateService);
const machineId = await resolveNodeMachineId(stateService, logService);
stateService.setItem(machineIdKey, machineId);
return machineId;
}
5 changes: 3 additions & 2 deletions src/vs/platform/telemetry/node/telemetryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@

import { isMacintosh } from 'vs/base/common/platform';
import { getMachineId } from 'vs/base/node/id';
import { ILogService } from 'vs/platform/log/common/log';
import { IStateReadService } from 'vs/platform/state/node/state';
import { machineIdKey } from 'vs/platform/telemetry/common/telemetry';


export async function resolveMachineId(stateService: IStateReadService) {
export async function resolveMachineId(stateService: IStateReadService, logService: ILogService) {
// We cache the machineId for faster lookups
// and resolve it only once initially if not cached or we need to replace the macOS iBridge device
let machineId = stateService.getItem<string>(machineIdKey);
if (typeof machineId !== 'string' || (isMacintosh && machineId === '6c9d2bc8f91b89624add29c0abeae7fb42bf539fa1cdb2e3e57cd668fa9bcead')) {
machineId = await getMachineId();
machineId = await getMachineId(logService.error.bind(logService));
}

return machineId;
Expand Down
2 changes: 1 addition & 1 deletion src/vs/platform/windows/electron-main/windowImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ export class CodeWindow extends Disposable implements ICodeWindow {
}

const { installSourcePath } = this.environmentMainService;
const machineId = await resolveMachineId(this.stateService);
const machineId = await resolveMachineId(this.stateService, this.logService);

const config: ITelemetryServiceConfig = {
appenders,
Expand Down
2 changes: 1 addition & 1 deletion src/vs/server/node/serverServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export async function setupServerServices(connectionToken: ServerConnectionToken
const [, , machineId] = await Promise.all([
configurationService.initialize(),
userDataProfilesService.init(),
getMachineId()
getMachineId(logService.error.bind(logService))
]);

const extensionHostStatusService = new ExtensionHostStatusService();
Expand Down