-
Notifications
You must be signed in to change notification settings - Fork 83
Move Node telemetry consent to mxc_ffi #1251
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
Merged
Branden Bonaby (bbonaby)
merged 11 commits into
main
from
user/bbonaby/node-native-telemetry
Sep 24, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
12b915b
Bind Node telemetry host services
bbonaby 676f116
Document telemetry binding role
bbonaby 48dd7bd
Run Node telemetry consent on a worker
bbonaby 0b18702
Document telemetry worker roles
bbonaby 4027dd1
Test Node telemetry worker bridge
bbonaby ba5c64f
Move Node telemetry consent onto mxc_ffi
bbonaby 7dc85a9
Test Node telemetry host services
bbonaby 514c996
Simplify Node telemetry native bridge
bbonaby 0a0848d
Bound Node telemetry consent requests
bbonaby 8bce167
Harden Node telemetry consent completion
bbonaby e5a21a8
Preserve non-Windows telemetry behavior
bbonaby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| // Worker-thread entry point for the blocking native consent request. | ||
|
|
||
| import { parentPort, workerData } from 'node:worker_threads'; | ||
| import { MxcError } from '../errors.js'; | ||
| import { | ||
| requestTelemetryConsentJson, | ||
| } from './telemetry.js'; | ||
| import type { | ||
| TelemetryRequestWorkerData, | ||
| TelemetryRequestWorkerMessage, | ||
| } from './telemetry-request-worker.js'; | ||
|
|
||
| function serializeError(error: unknown) { | ||
| if (error instanceof MxcError) { | ||
| return { | ||
| code: error.code, | ||
| message: error.message, | ||
| operation: error.operation, | ||
| nativeCode: error.nativeCode, | ||
| remediation: error.remediation, | ||
| details: error.details, | ||
| }; | ||
| } | ||
| return { | ||
| code: 'backend_error' as const, | ||
| message: error instanceof Error ? error.message : String(error), | ||
| }; | ||
| } | ||
|
|
||
| const data = workerData as TelemetryRequestWorkerData; | ||
| let message: TelemetryRequestWorkerMessage; | ||
| try { | ||
| const decision = new Int32Array(data.decisionShared); | ||
| message = { | ||
| kind: 'payload', | ||
| payload: requestTelemetryConsentJson(data.locale, (promptJson) => { | ||
| Atomics.store(decision, 0, 0); | ||
| Atomics.store(decision, 1, 0); | ||
| parentPort!.postMessage({ kind: 'present', promptJson } satisfies TelemetryRequestWorkerMessage); | ||
| Atomics.wait(decision, 0, 0); | ||
| return Atomics.load(decision, 1); | ||
| }), | ||
| }; | ||
| } catch (error) { | ||
| message = { kind: 'error', error: serializeError(error) }; | ||
| } | ||
| parentPort!.postMessage(message); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| // Main-thread bridge for the blocking native consent presenter callback. | ||
|
|
||
| import { Worker } from 'node:worker_threads'; | ||
| import { MxcError, type MxcErrorFields } from '../errors.js'; | ||
| import { TELEMETRY_CONSENT_PRESENTER_ERROR } from './telemetry.js'; | ||
|
|
||
| export interface TelemetryRequestWorkerData { | ||
| locale?: string; | ||
| decisionShared: SharedArrayBuffer; | ||
| } | ||
|
|
||
| export type TelemetryRequestWorkerMessage = | ||
| | { kind: 'payload'; payload: string } | ||
| | { kind: 'present'; promptJson: string } | ||
| | { kind: 'error'; error: MxcErrorFields }; | ||
|
|
||
| export interface BindingTelemetryWorkerLike { | ||
| on(event: 'message', listener: (message: TelemetryRequestWorkerMessage) => void): this; | ||
| on(event: 'error', listener: (error: Error) => void): this; | ||
| on(event: 'exit', listener: (code: number) => void): this; | ||
| unref(): void; | ||
| terminate(): void; | ||
| } | ||
|
|
||
| type WorkerFactory = (data: TelemetryRequestWorkerData) => BindingTelemetryWorkerLike; | ||
|
|
||
| const DEFAULT_TELEMETRY_REQUEST_TIMEOUT_MS = 30_000; | ||
|
|
||
| const defaultWorkerFactory: WorkerFactory = (data) => new Worker( | ||
| new URL('./telemetry-request-worker-entry.js', import.meta.url), | ||
| { workerData: data, execArgv: [] }, | ||
| ); | ||
|
|
||
| let workerFactory = defaultWorkerFactory; | ||
|
|
||
| export function _setBindingTelemetryWorkerFactory(factory?: WorkerFactory): void { | ||
| workerFactory = factory ?? defaultWorkerFactory; | ||
| } | ||
|
|
||
| function serializeUnknownError(error: unknown): Error { | ||
| return error instanceof Error ? error : new Error(String(error)); | ||
| } | ||
|
|
||
| export function runTelemetryConsentRequestAsync( | ||
| locale: string | undefined, | ||
| presenter: (promptJson: string, signal: AbortSignal) => number | Promise<number>, | ||
| timeoutMs = DEFAULT_TELEMETRY_REQUEST_TIMEOUT_MS, | ||
| ): Promise<string> { | ||
| const decision = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * 2)); | ||
| return new Promise((resolve, reject) => { | ||
| const worker = workerFactory({ | ||
| locale, | ||
| decisionShared: decision.buffer as SharedArrayBuffer, | ||
| }); | ||
| let settled = false; | ||
| let presenterAbort: AbortController | null = null; | ||
| let presenterError: Error | undefined; | ||
| let decisionWritten = false; | ||
| let deadline: ReturnType<typeof setTimeout> | undefined; | ||
| let deadlineStartedAt = 0; | ||
| let deadlineRemainingMs = timeoutMs; | ||
|
|
||
| const writeDecision = (code: number): void => { | ||
| if (decisionWritten) { | ||
| return; | ||
| } | ||
| decisionWritten = true; | ||
| Atomics.store(decision, 1, code); | ||
| Atomics.store(decision, 0, 1); | ||
| Atomics.notify(decision, 0); | ||
| }; | ||
|
|
||
| const clearDeadline = (): void => { | ||
| if (deadline !== undefined) { | ||
| clearTimeout(deadline); | ||
| deadline = undefined; | ||
| } | ||
| }; | ||
|
|
||
| const pauseDeadline = (): void => { | ||
| if (deadline === undefined) { | ||
| return; | ||
| } | ||
| deadlineRemainingMs = Math.max( | ||
| 0, | ||
| deadlineRemainingMs - (Date.now() - deadlineStartedAt), | ||
| ); | ||
| clearDeadline(); | ||
| }; | ||
|
|
||
| const finish = (action: () => void) => { | ||
| if (settled) { | ||
| return; | ||
| } | ||
| settled = true; | ||
| clearDeadline(); | ||
| presenterAbort?.abort(); | ||
| if (!decisionWritten) { | ||
| writeDecision(TELEMETRY_CONSENT_PRESENTER_ERROR); | ||
| } | ||
| action(); | ||
| }; | ||
|
|
||
| const armDeadline = (): void => { | ||
| if (settled || deadline !== undefined) { | ||
| return; | ||
| } | ||
| if (deadlineRemainingMs <= 0) { | ||
| finish(() => { | ||
| worker.unref(); | ||
| worker.terminate(); | ||
| reject(new Error('telemetry consent request timed out')); | ||
| }); | ||
| return; | ||
| } | ||
| deadlineStartedAt = Date.now(); | ||
| deadline = setTimeout(() => { | ||
| finish(() => { | ||
| worker.unref(); | ||
| worker.terminate(); | ||
| reject(new Error('telemetry consent request timed out')); | ||
| }); | ||
| }, deadlineRemainingMs); | ||
| }; | ||
|
|
||
| worker.on('message', (message) => { | ||
| if (message.kind === 'present') { | ||
| if (presenterAbort !== null) { | ||
| finish(() => reject(new Error('telemetry request worker requested presentation twice'))); | ||
| return; | ||
| } | ||
| pauseDeadline(); | ||
| presenterAbort = new AbortController(); | ||
| void (async () => { | ||
| let code = TELEMETRY_CONSENT_PRESENTER_ERROR; | ||
| try { | ||
| code = await presenter(message.promptJson, presenterAbort.signal); | ||
| if (!Number.isSafeInteger(code)) { | ||
| throw new Error(`consent presenter returned invalid decision '${String(code)}'`); | ||
| } | ||
| } catch (error) { | ||
| presenterError = serializeUnknownError(error); | ||
| code = TELEMETRY_CONSENT_PRESENTER_ERROR; | ||
| } | ||
| armDeadline(); | ||
| writeDecision(code); | ||
| })(); | ||
| return; | ||
| } | ||
| if (message.kind === 'payload') { | ||
| finish(() => { | ||
| if (presenterError) { | ||
| reject(presenterError); | ||
| } else { | ||
| resolve(message.payload); | ||
| } | ||
| }); | ||
| return; | ||
| } | ||
| if (message.kind === 'error') { | ||
| finish(() => reject(presenterError ?? new MxcError(message.error))); | ||
| return; | ||
| } | ||
| finish(() => reject(new Error('telemetry request worker returned an unexpected message'))); | ||
| }); | ||
| worker.on('error', (error) => finish(() => reject(error))); | ||
| worker.on('exit', (code) => finish(() => reject(presenterError ?? new MxcError({ | ||
| code: 'backend_error', | ||
| message: `telemetry worker exited before returning a result (code ${code})`, | ||
| })))); | ||
| armDeadline(); | ||
| }); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curiuos