Skip to content

Commit a4db350

Browse files
authored
Merge pull request #336 from PowerShell/rkeithhill/abort-dbg-attach-when-no-processid
Abort dbg attach when no valid processid str
2 parents 49f6e59 + 495e63d commit a4db350

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/PowerShellEditorServices.Protocol/DebugAdapter/AttachRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class AttachRequestArguments
1818
{
1919
public string ComputerName { get; set; }
2020

21-
public int ProcessId { get; set; }
21+
public string ProcessId { get; set; }
2222

2323
public int RunspaceId { get; set; }
2424
}

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,22 @@ protected async Task HandleAttachRequest(
207207
AttachRequestArguments attachParams,
208208
RequestContext<object> requestContext)
209209
{
210+
// If there are no host processes to attach to or the user cancels selection, we get a null for the process id.
211+
// This is not an error, just a request to stop the original "attach to" request.
212+
// Testing against "undefined" is a HACK because I don't know how to make "Cancel" on quick pick loading
213+
// to cancel on the VSCode side without sending an attachRequest with processId set to "undefined".
214+
if (string.IsNullOrEmpty(attachParams.ProcessId) || (attachParams.ProcessId == "undefined"))
215+
{
216+
Logger.Write(
217+
LogLevel.Normal,
218+
$"Attach request aborted, received {attachParams.ProcessId} for processId.");
219+
220+
await requestContext.SendError(
221+
"User aborted attach to PowerShell host process.");
222+
223+
return;
224+
}
225+
210226
StringBuilder errorMessages = new StringBuilder();
211227

212228
if (attachParams.ComputerName != null)
@@ -235,7 +251,8 @@ await requestContext.SendError(
235251
}
236252
}
237253

238-
if (attachParams.ProcessId > 0)
254+
int processId;
255+
if (int.TryParse(attachParams.ProcessId, out processId) && (processId > 0))
239256
{
240257
PowerShellVersionDetails runspaceVersion =
241258
this.editorSession.PowerShellContext.CurrentRunspace.PowerShellVersion;
@@ -249,13 +266,13 @@ await requestContext.SendError(
249266
}
250267

251268
await this.editorSession.PowerShellContext.ExecuteScriptString(
252-
$"Enter-PSHostProcess -Id {attachParams.ProcessId}",
269+
$"Enter-PSHostProcess -Id {processId}",
253270
errorMessages);
254271

255272
if (errorMessages.Length > 0)
256273
{
257274
await requestContext.SendError(
258-
$"Could not attach to process '{attachParams.ProcessId}'");
275+
$"Could not attach to process '{processId}'");
259276

260277
return;
261278
}
@@ -272,6 +289,10 @@ await requestContext.SendError(
272289
}
273290
else
274291
{
292+
Logger.Write(
293+
LogLevel.Error,
294+
$"Attach request failed, '{attachParams.ProcessId}' is an invalid value for the processId.");
295+
275296
await requestContext.SendError(
276297
"A positive integer must be specified for the processId field.");
277298

0 commit comments

Comments
 (0)