Repository navigation
Make telemetry initialization and disposal best-effort so it can never fail a build - #14554
Conversation
Fixes the MSB1025 build failure caused by a NullReferenceException escaping VsTelemetryInitializer.Dispose(), and stops BuildManager.Dispose() from tearing down the process-wide telemetry session it does not own. Co-authored-by: AlesProkop <[email protected]>
Address three review findings on the telemetry best-effort change. Do not swallow critical exceptions. Both the initialization and disposal handlers used a bare catch, which also absorbed OutOfMemoryException, AccessViolationException and InternalErrorException. MSBuild classifies those as critical in ExceptionHandling.IsCriticalException, so the build could report success while the process was already unrecoverable. Both handlers now filter on !IsCriticalException; NullReferenceException is deliberately not critical, so the MSB1025 fix is unaffected. Do not leak a partially initialized session. Ownership was recorded only after UseVsIsOptedIn() and Start() had both succeeded, so a session that was created and then failed to start was never tracked and never disposed - it stayed referenced by the telemetry SDK for the lifetime of the process. Ownership is now recorded immediately after creation, so shutdown cleans up regardless of where initialization failed. Cover the reported crash. The existing tests never disposed an owned TelemetrySession, so they passed against the old type-filtered catch and could not detect a regression of the reported failure. The new net472 test poisons the session with an uninitialized instance whose Dispose() throws NullReferenceException, then asserts that disposal is contained and that session and ownership state are cleared. Verified to fail with the previous filter, reproducing the exact NullReferenceException from the issue, while the other telemetry tests still passed. Co-authored-by: Copilot <[email protected]> Copilot-Session: fbd1a9a9-a7e3-4f3a-ae4c-cfe365e7addf
There was a problem hiding this comment.
Pull request overview
This PR hardens MSBuild’s telemetry lifecycle so that failures inside the VS telemetry stack (especially on .NET Framework) cannot surface as build failures, and ensures process-wide telemetry isn’t torn down by BuildManager instances that don’t own it.
Changes:
- Make telemetry initialization and disposal best-effort by swallowing all non-critical exceptions and clearing activity state before teardown.
- Fix VS telemetry session initialization to handle
CreateAndGetDefaultSession(...)returningnull, and make disposal idempotent by clearing static state before calling into the SDK. - Stop
BuildManager.Dispose()from disposing the process-wide telemetry manager; add unit tests covering idempotent/best-effort disposal and ownership boundaries.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Framework/Telemetry/TelemetryManager.cs | Catch-all best-effort init/dispose; clear activity source and make VS session init/dispose safer. |
| src/Framework.UnitTests/TelemetryManager_Tests.cs | New unit tests validating best-effort disposal, idempotency, and reset behavior. |
| src/Build/BackEnd/BuildManager/BuildManager.cs | Remove telemetry teardown from BuildManager disposal (ownership fix). |
| src/Build.UnitTests/Telemetry/Telemetry_Tests.cs | New test ensuring disposing BuildManager does not dispose process-wide telemetry. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
rainersigwald
left a comment
There was a problem hiding this comment.
Looks reasonable but raises the specter of "now something is failing which has killed 100% of our telemetry". What are we doing to keep track of that--should we add a release task at build promotion time to validate that we see telemetry from the prerel version?
Context
MSBuild 18.8.2 on .NET Framework fails builds with
MSBUILD : error MSB1025when aNullReferenceExceptionescapesVsTelemetryInitializer.Dispose(). Two independent defects combine here:TelemetryManagerswallowed the wrong exceptions. Both init and dispose used a filtered catch (FileNotFoundException/FileLoadException/TypeLoadException), which doesn't match an NRE thrown from inside the VS telemetry stack. The exception propagated up throughBuildManager.Dispose()toMSBuildApp.Executeand failed the build.BuildManagerdisposed a session it doesn't own. The telemetry session is process-wide, created by the entry point (XMake.Main) or the host (VS).MSBuildApp.BuildProject'sfinallydisposesBuildManager.DefaultBuildManager, so the session was torn down while the process was still running — leavingCrashTelemetryRecorder.PostFaultEventposting to a disposed session, which is the second stack trace in the report.Telemetry is best-effort infrastructure; no failure inside it should be observable by a build.
Changes Made
src/Framework/Telemetry/TelemetryManager.csTryInitializeTelemetryandDisposenow use a catch-all instead of a type-filtered catch. Comments record which exceptions are expected and why the filter isn't sufficient.s_disposedis set before the teardown work, andDefaultActivitySourceis cleared, so nothing can emit through a source backed by a disposed session.VsTelemetryInitializer.Initializeno longer dereferences the session unconditionally.CreateAndGetDefaultSessioncan return null (e.g. telemetry disabled machine-wide), soUseVsIsOptedIn()/Start()and ownership are only taken when a session actually exists:VsTelemetryInitializer.Disposesnapshots and clears the static state before disposing, so a throwingDispose()can't leave a stale session behind and a repeat call is a no-op.using System;/using System.IO;, now unused (IDE0005 is warning-as-error in official builds).src/Build/BackEnd/BuildManager/BuildManager.csTelemetryManager.Instance.Dispose()fromDispose(bool). This is a no-op change for hosts, which initialize withisStandalone: falseand therefore never owned the session;XMake.MainandMSBuild.Coordinator/Program.csalready pair their own Initialize/Dispose, and on .NET CoreDispose()has no body.Testing
src/Framework.UnitTests/TelemetryManager_Tests.cs: disposal without initialization doesn't throw, disposal is idempotent,ResetForTestclears state.BuildManagerDisposeDoesNotDisposeProcessWideTelemetryinsrc/Build.UnitTests/Telemetry/Telemetry_Tests.cs, verified to fail when the removedTelemetryManager.Instance.Dispose()call is restored.Notes
This is defensive. The underlying reason
TelemetrySession.Dispose()NREs in the reporter's environment (Windows Server 2022, VS 2026 + VS 2022 side-by-side,VS_TELEMETRY_OPT_OUT=1) is in the closed-source VS Telemetry SDK and is not addressed here — but no failure mode inside it can fail a build anymore.Worth a look during review:
BuildManager.EndBuildTelemetry()callsStartActivitywithout a guard. Left alone as out of scope, but it's adjacent.