Skip to content

Commit d48438c

Browse files
Fix breakpoint setting deadlock (#1112)
* Fix breakpoint setting deadlock * codacy issues
1 parent 77922a4 commit d48438c

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/PowerShellEditorServices/Services/DebugAdapter/DebugEventHandlerService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private void DebugService_BreakpointUpdated(object sender, BreakpointUpdatedEven
128128
{
129129
string reason = "changed";
130130

131-
if (_debugStateService.SetBreakpointInProgress)
131+
if (_debugStateService.IsSetBreakpointInProgress)
132132
{
133133
// Don't send breakpoint update notifications when setting
134134
// breakpoints on behalf of the client.

src/PowerShellEditorServices/Services/DebugAdapter/DebugStateService.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.PowerShell.EditorServices.Utility;
9+
610
namespace Microsoft.PowerShell.EditorServices.Services
711
{
812
internal class DebugStateService
913
{
14+
private readonly SemaphoreSlim _setBreakpointInProgressHandle = AsyncUtils.CreateSimpleLockingSemaphore();
15+
1016
internal bool NoDebug { get; set; }
1117

1218
internal string Arguments { get; set; }
@@ -25,8 +31,20 @@ internal class DebugStateService
2531

2632
internal bool IsInteractiveDebugSession { get; set; }
2733

28-
internal bool SetBreakpointInProgress { get; set; }
34+
// If the CurrentCount is equal to zero, then we have some thread using the handle.
35+
internal bool IsSetBreakpointInProgress => _setBreakpointInProgressHandle.CurrentCount == 0;
2936

3037
internal bool IsUsingTempIntegratedConsole { get; set; }
38+
39+
internal void ReleaseSetBreakpointHandle()
40+
{
41+
_setBreakpointInProgressHandle.Release();
42+
}
43+
44+
internal async Task WaitForSetBreakpointHandleAsync()
45+
{
46+
await _setBreakpointInProgressHandle.WaitAsync()
47+
.ConfigureAwait(continueOnCapturedContext: false);
48+
}
3149
}
3250
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public async Task<SetFunctionBreakpointsResponse> Handle(SetFunctionBreakpointsA
4848
CommandBreakpointDetails[] updatedBreakpointDetails = breakpointDetails;
4949
if (!_debugStateService.NoDebug)
5050
{
51-
_debugStateService.SetBreakpointInProgress = true;
51+
await _debugStateService.WaitForSetBreakpointHandleAsync();
5252

5353
try
5454
{
@@ -63,7 +63,7 @@ await _debugService.SetCommandBreakpointsAsync(
6363
}
6464
finally
6565
{
66-
_debugStateService.SetBreakpointInProgress = false;
66+
_debugStateService.ReleaseSetBreakpointHandle();
6767
}
6868
}
6969

@@ -196,7 +196,7 @@ public async Task<SetBreakpointsResponse> Handle(SetBreakpointsArguments request
196196
BreakpointDetails[] updatedBreakpointDetails = breakpointDetails;
197197
if (!_debugStateService.NoDebug)
198198
{
199-
_debugStateService.SetBreakpointInProgress = true;
199+
await _debugStateService.WaitForSetBreakpointHandleAsync();
200200

201201
try
202202
{
@@ -212,7 +212,7 @@ await _debugService.SetLineBreakpointsAsync(
212212
}
213213
finally
214214
{
215-
_debugStateService.SetBreakpointInProgress = false;
215+
_debugStateService.ReleaseSetBreakpointHandle();
216216
}
217217
}
218218

0 commit comments

Comments
 (0)