Skip to content

Fix remote debug adapter process lifecycle - #8650

Open
Stella Huang (StellaHuang95) wants to merge 1 commit into
microsoft:mainfrom
StellaHuang95:stellahuang-microsoft-remote-attach-lifecycle
Open

Stella Huang (StellaHuang95) wants to merge 1 commit into
microsoft:mainfrom
StellaHuang95:stellahuang-microsoft-remote-attach-lifecycle

Conversation

@StellaHuang95

Copy link
Copy Markdown
Contributor

Summary

  • Correct DebugAdapterRemoteProcess.HasExited so a connected remote adapter is reported as running.
  • Route DAP disconnect, socket EOF/reset, stream disposal, Terminate, and Dispose through one thread-safe/idempotent exit transition.
  • Dispose the owned socket/stream before raising Exited, and raise Exited exactly once under concurrent close paths.
  • Preserve the Visual Studio UI-thread JoinableTaskFactory connection wait while making timeout and socket-failure cleanup deterministic.
  • Parse bounded DAP frames across arbitrary read boundaries so fragmented disconnect responses are recognized reliably.
  • Add focused loopback coverage through the ITargetHostProcess contract.

Fixes #8305

Before / after

Before: a successful connection set _debuggerConnected = true, and HasExited returned that value. Debug Adapter Host therefore saw the newly connected adapter as already exited. Disconnect/terminate/dispose paths independently disposed the stream, did not consistently update state, and could miss or duplicate lifecycle notification.

After: connected means HasExited == false. The first disconnect/terminate/dispose transition atomically marks the process exited, unsubscribes stream callbacks, disposes the transport, and raises Exited; later concurrent transitions are no-ops.

Validation

  • Built Python\Tests\Core\PythonToolsTests.csproj with VS 2022 MSBuild targeting VS 17.0.
  • Ran DebugAdapterRemoteProcessTests plus LauncherTests: 15/15 passed.
  • Repeated the eight lifecycle tests 20 times: 160/160 passed.
  • Loopback tests cover connected state, EOF, fragmented DAP disconnect after a prior message, terminate, dispose, external stream disposal, concurrent disconnect/disposal, timeout cleanup, and socket-failure cleanup.

Manual remote debugpy attach

Ran:

py -3.14 C:\Users\stellahuang\.copilot\session-state\47b8fa60-3d73-4680-8781-5e56196aefc8\files\issue-8305-debugpy-e2e.py

The harness spawned:

C:\Users\stellahuang\AppData\Local\Programs\Python\Python314\python.exe -m debugpy --listen 127.0.0.1:57517 --wait-for-client C:\Users\stellahuang\.copilot\session-state\47b8fa60-3d73-4680-8781-5e56196aefc8\files\issue-8305-remote-target.py

Using Python 3.14.7 and debugpy 1.8.20, two sequential DAP attach sessions connected to the same live target process. Both sessions returned MainThread, verified and hit the breakpoint at line 9, continued successfully, acknowledged disconnect, and emitted terminated. The target remained alive between sessions.

Review notes

The final race/resource review covered state ordering, reentrant event handlers, concurrent read/dispose, UI-thread waits, timeout task observation, socket ownership, DAP fragmentation, parser bounds, and exception behavior. It found and fixed two issues before publication: restoring JTF-based production waiting to avoid a UI responsiveness regression, and resetting both length and position of the reusable DAP frame buffer between messages.

Residual risk

The manual test exercised the real debugpy adapter and target over loopback, while the committed tests exercise the exact ITargetHostProcess transport/lifecycle contract. A full Visual Studio experimental-instance Attach to Process UI run was not available in this environment.

Make remote attach lifecycle transitions thread-safe and idempotent, detect
DAP and socket disconnects reliably, and cover timeout, failure, disposal,
and concurrent exit paths with loopback tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 47b8fa60-3d73-4680-8781-5e56196aefc8
@StellaHuang95
Stella Huang (StellaHuang95) requested a review from a team as a code owner October 22, 2026 19:37
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@sonarqubecloud

Copy link
Copy Markdown

@heejaechang

Heejae Chang (heejaechang) commented Oct 22, 2026 •

Copy link
Copy Markdown

🔒 Automated review in progress — Heejae Chang (@heejaechang) is auto-reviewing this PR.

@StellaHuang95

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@heejaechang

Copy link
Copy Markdown

Result: 🔴 could-not-verify

Verification details

Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified.

Details: Verification could not complete: timeout must be None or at least 2073600 seconds (24 days); shorter completion deadlines are unsupported.

Test runs: none recorded.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved via Review Center.

@heejaechang Heejae Chang (heejaechang) added the review-auto:approved Automated review: no blocking findings (approval posted). label Oct 22, 2026

@bschnurr Bill Schnurr (bschnurr) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved via Review Center.

@rchiodo Rich Chiodo (rchiodo) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved via Review Center.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review-auto:approved Automated review: no blocking findings (approval posted).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Visual Studio Insiders Python debugpy attach to remote process fails to get target process.

4 participants