PowerShell / PowerShell/PowerShellEditorServices

Remote PSSession disconnects after launch debugging session ends

Đang mở
#2,263 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Issue-Bug Needs: Triage
Ngôn ngữ chính
C#
Star
767
Fork
266
Merge trung bình
3 ngày 16 giờ
Pull request đã merge (30 ngày)
1

Mô tả

Prerequisites
  • I have written a descriptive issue title.
  • I have searched all open and closed issues to ensure it has not already been reported.
  • I have read the troubleshooting guide.
  • I am sure this issue is with PowerShell Editor Services itself and does not reproduce in a standalone PowerShell instance, and is not an issue with my editor.
  • I have verified that I am using the latest version of PowerShell Editor Services.
  • If this is a security issue, I have read the security issue reporting guidance.
Summary

When using remote PowerShell debugging from VS Code server (Linux → Windows over PSSession over SSH) with a launch configuration and path mappings, the remote PSSession unexpectedly drops immediately after the debug session finishes. As soon as any command is run post-debugging, the user gets kicked back to the local session.

Technical Analysis

After investigating the PowerShellEditorServices codebase, I traced the issue through this execution flow:

  1. Launch sessions never set ExecutionCompleted = true when they finish normally. Unlike Attach sessions (which set this via OnExecutionCompletedAsync in LaunchAndAttachHandler.cs:493), Launch sessions execute via ConfigurationDoneHandler.LaunchScriptAsync() which has no equivalent completion handler.

  2. When the debug session ends, DisconnectHandler checks ExecutionCompleted (DisconnectHandler.cs:55-58)

  3. Because ExecutionCompleted is still false, it calls _debugService.Abort()

  4. Abort() calls SetDebugResuming(DebuggerResumeAction.Stop) (PowerShellDebugContext.cs:97)

  5. SetDebugResuming() calls _psesHost.SetExit() (PowerShellDebugContext.cs:115)

  6. SetExit() sets _shouldExit = true on the remote frame when it's a REPL with multiple frames (PsesInternalHost.cs:360-371)

  7. The next time the run loop executes (RunExecutionLoop, around line 917), it sees _shouldExit == true for a remote, non-nested REPL frame and calls PopPowerShell() (PsesInternalHost.cs:917-922)

  8. PopPowerShell() pops the remote REPL frame and detects the runspace change, which causes it to pop from _runspaceStack, exiting the PSSession (PsesInternalHost.cs:763-790)

Root Cause

Launch sessions do not set ExecutionCompleted = true when the debugged script finishes. This causes DisconnectHandler to treat a normal completion as an abort scenario, calling Abort() which ultimately sets _shouldExit = true on the remote REPL frame, causing the PSSession to exit.

Proposed Fix

Set ExecutionCompleted = true in ConfigurationDoneHandler.LaunchScriptAsync() after script execution completes:

await _executionService.ExecutePSCommandAsync(
    command,
    CancellationToken.None,
    s_debuggerExecutionOptions).ConfigureAwait(false);

// Fix: Mark execution as completed for launch sessions
if (_debugStateService is not null)
{
    _debugStateService.ExecutionCompleted = true;
}

_debugAdapterServer?.SendNotification(EventNames.Terminated);

This aligns the behavior of Launch sessions with Attach sessions, which already set this flag in OnExecutionCompletedAsync.

PowerShell Version
$PSVersionTable        

Name                           Value
----                           -----
PSVersion                      7.5.4
PSEdition                      Core
GitCommitId                    7.5.4
OS                             Rocky Linux 8.10 (Green Obsidian)
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Editor Version
emacs --version

GNU Emacs 30.1.90
PowerShell Editor Services Version
$pseditor.EditorServicesVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
4      4      0      0
Steps to Reproduce

Steps to Reproduce

Set up VS Code server on Linux
Configure launch debugging with path mappings
Establish remote PSSession to Windows machine over SSH
Set breakpoint
Start a launch-based debug session
Let the debug session complete normally
Run any command in the remote session
Observe that the PSSession drops and returns to local session
Expected Behavior
The remote PSSession should remain active after a launch debug session completes, allowing continued work in the remote context.

Actual Behavior
The remote PSSession drops immediately after the first command is run following debug session completion.

Visuals

No response

Logs

No response

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu trong ConfigurationDoneHandler.LaunchScriptAsync và so sánh cách xử lý hoàn tất của nó với OnExecutionCompletedAsync trong LaunchAndAttachHandler.cs; sau đó theo dõi DisconnectHandler.cs và vòng lặp chạy của PsesInternalHost.cs được mô tả trong báo cáo. Thay đổi được hoàn tất khi một phiên gỡ lỗi launch kết thúc bình thường vẫn giữ PSSession từ xa hoạt động để thực hiện lệnh tiếp theo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
csharp, powershell, vscode
Lĩnh vực
devtools
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
55/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.