Skip to content

[file-diet] Refactor HangDumpProcessLifetimeHandler.cs (1045 lines) into focused partial files #10781

Description

@github-actions

Overview

The file src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs has grown to 1045 lines, making it harder to navigate and maintain. This task involves refactoring it into smaller, more focused files.

Current State

  • File: src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs
  • Size: 1045 lines
  • Language: C#
Structural Analysis

The file contains two top-level types:

  1. HangDumpProcessLifetimeHandler (internal sealed class, ~990 lines) implementing ITestHostProcessLifetimeHandler, IOutputDeviceDataProducer, IDataProducer, IAsyncDisposable/IDisposable. It mixes several distinct concerns:
    • Core lifecycle members: constructor, fields, IsEnabledAsync, BeforeTestHostProcessStartAsync, CallbackAsync, OnTestHostProcessStartedAsync, OnTestHostProcessExitedAsync, timer/deadline handling (OnDeadlineTimerElapsed, GetTimerDueTime), and Dispose/DisposeAsync.
    • Process-tree diagnostics helpers: GetProcessTreeWithTimeoutAsync, QueryOnceAndDumpTreeAsync, TryGetProcessById, GetInProgressTestsAsync, QueryInProgressTestsWithTimeoutAsync, RunBestEffortDiagnosticAsync, GetDiskInfo.
    • Dump-taking logic: TriggerDumpOnce, TakeDumpOfTreeAsync, TakeDumpAsync, GetDumpFileNames/DumpFileNames, EnsureProcessIdPlaceholder, GetDumpFileNamePattern.
  2. OutputDeviceWriter (internal sealed class, ~20 lines) — an unrelated helper for writing output device data, tacked onto the end of the same file.

Refactoring Strategy

Proposed File Splits

  1. HangDumpProcessLifetimeHandler.cs (keep, trimmed to ~350–400 lines)

    • Contents: fields, constructor, properties (Uid, Version, DisplayName, Description, DataTypesProduced), IsEnabledAsync, BeforeTestHostProcessStartAsync, CallbackAsync, OnTestHostProcessStartedAsync, OnTestHostProcessExitedAsync, OnDeadlineTimerElapsed, GetTimerDueTime, Dispose, DisposeAsync
    • Responsibility: Core ITestHostProcessLifetimeHandler lifecycle orchestration and disposal.
  2. HangDumpProcessLifetimeHandler.ProcessTree.cs (partial class)

    • Contents: GetProcessTreeWithTimeoutAsync, QueryOnceAndDumpTreeAsync, TryGetProcessById, GetInProgressTestsAsync, QueryInProgressTestsWithTimeoutAsync, RunBestEffortDiagnosticAsync, GetDiskInfo
    • Responsibility: Process-tree discovery and best-effort diagnostic querying.
  3. HangDumpProcessLifetimeHandler.DumpTaking.cs (partial class)

    • Contents: TriggerDumpOnce, TakeDumpOfTreeAsync, TakeDumpAsync, GetDumpFileNames, DumpFileNames, EnsureProcessIdPlaceholder, GetDumpFileNamePattern, NotifyCrashDumpServiceIfEnabled
    • Responsibility: Triggering and producing the actual hang dump artifacts.
  4. OutputDeviceWriter.cs

    • Contents: OutputDeviceWriter class
    • Responsibility: Thin wrapper for writing IOutputDeviceData — unrelated to hang-dump logic and should live in its own file.

Implementation Guidelines

  1. Preserve Behavior: All existing functionality must work identically after the split
  2. Maintain Public API: Keep exported/public symbols accessible with the same names (mark the class partial where split across files)
  3. Update Imports: Fix all import paths/usings throughout the split files
  4. Test After Each Split: Run the test suite after each incremental change
  5. One File at a Time: Split one module at a time to make review easier

Acceptance Criteria

  • Original file is split into focused modules (using partial class for HangDumpProcessLifetimeHandler)
  • Each new file is under 300 lines where practical
  • All tests pass after refactoring
  • No breaking changes to public API
  • All import paths updated correctly

Priority: Medium
Effort: Medium — mechanical extraction using partial classes with test verification at each step
Expected Impact: Improved code navigability, easier testing, reduced merge conflicts

🤖 Automated content by GitHub Copilot. Generated by the Daily File Diet workflow. · auto · 25.5 AIC · ⌖ 1.64 AIC · ⊞ 11.4K · [◷]( · )

  • expires on Aug 28, 2026, 7:28 PM UTC

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/dumpCrashDump / HangDump extensions.type/automationCreated or maintained by an agentic workflow.type/tech-debtCode health, refactoring, simplification.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions