@@ -207,6 +207,22 @@ protected async Task HandleAttachRequest(
207
207
AttachRequestArguments attachParams ,
208
208
RequestContext < object > requestContext )
209
209
{
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
+
210
226
StringBuilder errorMessages = new StringBuilder ( ) ;
211
227
212
228
if ( attachParams . ComputerName != null )
@@ -235,7 +251,8 @@ await requestContext.SendError(
235
251
}
236
252
}
237
253
238
- if ( attachParams . ProcessId > 0 )
254
+ int processId ;
255
+ if ( int . TryParse ( attachParams . ProcessId , out processId ) && ( processId > 0 ) )
239
256
{
240
257
PowerShellVersionDetails runspaceVersion =
241
258
this . editorSession . PowerShellContext . CurrentRunspace . PowerShellVersion ;
@@ -249,13 +266,13 @@ await requestContext.SendError(
249
266
}
250
267
251
268
await this . editorSession . PowerShellContext . ExecuteScriptString (
252
- $ "Enter-PSHostProcess -Id { attachParams . ProcessId } ",
269
+ $ "Enter-PSHostProcess -Id { processId } ",
253
270
errorMessages ) ;
254
271
255
272
if ( errorMessages . Length > 0 )
256
273
{
257
274
await requestContext . SendError (
258
- $ "Could not attach to process '{ attachParams . ProcessId } '") ;
275
+ $ "Could not attach to process '{ processId } '") ;
259
276
260
277
return ;
261
278
}
@@ -272,6 +289,10 @@ await requestContext.SendError(
272
289
}
273
290
else
274
291
{
292
+ Logger . Write (
293
+ LogLevel . Error ,
294
+ $ "Attach request failed, '{ attachParams . ProcessId } ' is an invalid value for the processId.") ;
295
+
275
296
await requestContext . SendError (
276
297
"A positive integer must be specified for the processId field." ) ;
277
298
0 commit comments