Skip to content

Commit 0fd091d

Browse files
committed
Use temp threads handler
1 parent 8debfc1 commit 0fd091d

File tree

3 files changed

+67
-12
lines changed

3 files changed

+67
-12
lines changed

src/PowerShellEditorServices/Server/PsesDebugServer.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,15 @@ public async Task StartAsync()
9898
.WithInput(_inputStream)
9999
.WithOutput(_outputStream)
100100
.WithServices(serviceCollection => serviceCollection
101+
.AddSingleton(_loggerFactory)
101102
.AddLogging()
102103
.AddOptions()
103104
.AddPsesDebugServices(ServiceProvider, this, _useTempSession))
104105
.WithHandler<LaunchAndAttachHandler>()
105106
.WithHandler<DisconnectHandler>()
106107
.WithHandler<BreakpointHandlers>()
107108
.WithHandler<ConfigurationDoneHandler>()
108-
.WithHandler<ThreadsHandler>()
109+
.WithHandler<ThreadsTempHandler>()
109110
.WithHandler<StackTraceHandler>()
110111
.WithHandler<ScopesHandler>()
111112
.WithHandler<VariablesHandler>()

src/PowerShellEditorServices/Services/DebugAdapter/DebugEventHandlerService.cs

+8-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System.Management.Automation;
77
using Microsoft.Extensions.Logging;
8+
using Microsoft.PowerShell.EditorServices.Handlers;
89
using Microsoft.PowerShell.EditorServices.Services.DebugAdapter;
910
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;
1011
using Microsoft.PowerShell.EditorServices.Utility;
@@ -70,20 +71,20 @@ private void DebugService_DebuggerStopped(object sender, DebuggerStoppedEventArg
7071
// "step", "breakpoint", "function breakpoint", "exception" and "pause".
7172
// We don't support exception breakpoints and for "pause", we can't distinguish
7273
// between stepping and the user pressing the pause/break button in the debug toolbar.
73-
string debuggerStoppedReason = "step";
74+
StoppedEventReason debuggerStoppedReason = StoppedEventReason.Step;
7475
if (e.OriginalEvent.Breakpoints.Count > 0)
7576
{
7677
debuggerStoppedReason =
7778
e.OriginalEvent.Breakpoints[0] is CommandBreakpoint
78-
? "function breakpoint"
79-
: "breakpoint";
79+
? StoppedEventReason.FunctionBreakpoint
80+
: StoppedEventReason.Breakpoint;
8081
}
8182

8283
_debugAdapterServer.SendNotification(EventNames.Stopped,
8384
new StoppedEvent
8485
{
85-
ThreadId = 1,
86-
Reason = debuggerStoppedReason
86+
Reason = debuggerStoppedReason,
87+
ThreadId = ThreadsTempHandler.PipelineThread.Id,
8788
});
8889
}
8990

@@ -108,8 +109,7 @@ private void PowerShellContext_RunspaceChanged(object sender, RunspaceChangedEve
108109
_debugAdapterServer.SendNotification(EventNames.Continued,
109110
new ContinuedEvent
110111
{
111-
ThreadId = 1,
112-
AllThreadsContinued = true
112+
ThreadId = ThreadsTempHandler.PipelineThread.Id,
113113
});
114114
}
115115
}
@@ -119,8 +119,7 @@ private void PowerShellContext_DebuggerResumed(object sender, DebuggerResumeActi
119119
_debugAdapterServer.SendNotification(EventNames.Continued,
120120
new ContinuedEvent
121121
{
122-
AllThreadsContinued = true,
123-
ThreadId = 1
122+
ThreadId = ThreadsTempHandler.PipelineThread.Id,
124123
});
125124
}
126125

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/ThreadsHandler.cs

+57-2
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,77 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6+
using System.ComponentModel;
67
using System.Threading;
78
using System.Threading.Tasks;
9+
using MediatR;
10+
using OmniSharp.Extensions.DebugAdapter.Protocol;
11+
using OmniSharp.Extensions.DebugAdapter.Protocol.Client;
812
using OmniSharp.Extensions.DebugAdapter.Protocol.Models;
913
using OmniSharp.Extensions.DebugAdapter.Protocol.Requests;
14+
using OmniSharp.Extensions.DebugAdapter.Protocol.Server;
15+
using OmniSharp.Extensions.JsonRpc;
16+
using OmniSharp.Extensions.JsonRpc.Generation;
17+
using Thread = OmniSharp.Extensions.DebugAdapter.Protocol.Models.Thread;
18+
19+
namespace System.Runtime.CompilerServices
20+
{
21+
[EditorBrowsable(EditorBrowsableState.Never)]
22+
public class IsExternalInit{}
23+
}
24+
25+
namespace OmniSharp.Extensions.DebugAdapter.Protocol
26+
{
27+
28+
[Parallel]
29+
[Method(RequestNames.Threads, Direction.ClientToServer)]
30+
[
31+
GenerateHandler,
32+
GenerateHandlerMethods(typeof(IDebugAdapterServerRegistry)),
33+
GenerateRequestMethods(typeof(IDebugAdapterClient)),
34+
]
35+
public record ThreadsTempArguments : IRequest<ThreadsTempResponse>
36+
{
37+
}
38+
39+
public record ThreadsTempResponse
40+
{
41+
public Container<Thread>? Threads { get; init; }
42+
}
43+
44+
}
1045

1146
namespace Microsoft.PowerShell.EditorServices.Handlers
1247
{
48+
/*
1349
internal class ThreadsHandler : IThreadsHandler
1450
{
51+
internal static Thread MainDebuggedThread { get; } = new Thread(() => { });
52+
1553
public Task<ThreadsResponse> Handle(ThreadsArguments request, CancellationToken cancellationToken)
1654
{
1755
return Task.FromResult(new ThreadsResponse
1856
{
19-
// TODO: This is an empty container of threads...do we need to make a thread?
20-
Threads = new Container<System.Threading.Thread>()
57+
// TODO: Omnisharp supports multithreaded debugging (where multiple threads can be debugged at once), but we don't.
58+
// This means we always need to set AllThreadsStoppped and AllThreadsContinued in our events.
59+
// But if we one day support multithreaded debugging, we'd need a way to associate debugged runspaces with .NET threads in a consistent way
60+
Threads = new Container<Thread>(MainDebuggedThread),
2161
});
2262
}
2363
}
64+
*/
65+
66+
internal class ThreadsTempHandler : ThreadsTempHandlerBase
67+
{
68+
internal static Thread PipelineThread { get; } = new Thread { Id = 1, Name = "PowerShell Pipeline Thread" };
69+
70+
public override Task<ThreadsTempResponse> Handle(ThreadsTempArguments request, CancellationToken cancellationToken)
71+
{
72+
return Task.FromResult(
73+
new ThreadsTempResponse
74+
{
75+
Threads = new Container<Thread>(PipelineThread)
76+
});
77+
}
78+
}
2479
}

0 commit comments

Comments
 (0)