@@ -1903,9 +1903,12 @@ export namespace Telemetry {
19031903 // once by the stamp comparison; the backoff only throttles retries of the unchanged one.
19041904 const IDENTITY_RETRY_MS = 60_000
19051905 let identityRetryAt = 0 // 0: no retry pending
1906- // altimate_change — bumped by shutdown(); a detached opt-out send started under an earlier
1907- // lifecycle neither posts nor writes its marker once a later init may have cleared it.
1906+ // altimate_change start — bumped by shutdown(); a detached opt-out send started under an earlier
1907+ // lifecycle does not post. Its marker write keys on markerGeneration instead, which only an
1908+ // enabled init bumps: a send accepted across a shutdown while still opted out keeps its marker.
19081909 let lifecycleGeneration = 0
1910+ let markerGeneration = 0
1911+ // altimate_change end
19091912 let machineId = ""
19101913 let sessionId = ""
19111914 let projectId = ""
@@ -2151,7 +2154,8 @@ export namespace Telemetry {
21512154 machineId = getOrCreateMachineId ( )
21522155 enabled = true
21532156 // altimate_change — telemetry is on: forget the opt-out marker, so turning it off again is
2154- // recorded again.
2157+ // recorded again. The bump first, so a send still in flight never recreates it.
2158+ markerGeneration ++
21552159 OptOutMarker . clear ( )
21562160 log . info ( "telemetry initialized" , { mode : "appinsights" } )
21572161 // altimate_change — clear any existing interval before installing a new one. doInit() can
@@ -2230,6 +2234,7 @@ export namespace Telemetry {
22302234 }
22312235 async function recordOptOut ( source : "env" | "config" ) : Promise < void > {
22322236 const lifecycle = lifecycleGeneration
2237+ const marker = markerGeneration
22332238 try {
22342239 if ( underTestRunner ( ) && ! optOutUnderTestRunner ) return
22352240 // The force flag is a developer knob for running telemetry under a test runner; the opt-out
@@ -2246,8 +2251,8 @@ export namespace Telemetry {
22462251 cfg ,
22472252 { machineId : id , identity : await TelemetryIdentity . cached ( ) } ,
22482253 )
2249- // A shutdown since this task started means a later init may own the marker now: neither
2250- // send nor write , and let the next disabled init start over.
2254+ // A shutdown since this task started means a later init may own the marker now: do not
2255+ // send, and let the next disabled init start over.
22512256 if ( lifecycle !== lifecycleGeneration ) return
22522257 const controller = new AbortController ( )
22532258 const timeout = setTimeout ( ( ) => controller . abort ( ) , OPT_OUT_TIMEOUT_MS )
@@ -2258,7 +2263,8 @@ export namespace Telemetry {
22582263 body : JSON . stringify ( envelopes ) ,
22592264 signal : controller . signal ,
22602265 } )
2261- if ( res . ok && lifecycle === lifecycleGeneration ) OptOutMarker . write ( )
2266+ // Accepted: write the marker unless telemetry was turned back on meanwhile.
2267+ if ( res . ok && marker === markerGeneration ) OptOutMarker . write ( )
22622268 } finally {
22632269 clearTimeout ( timeout )
22642270 }
0 commit comments