@@ -33,15 +33,11 @@ public partial class PSConsoleReadLine : IPSConsoleReadLineMockableMethods
33
33
{
34
34
private const int ConsoleExiting = 1 ;
35
35
36
- private const int CancellationRequested = 2 ;
37
-
38
36
// *must* be initialized in the static ctor
39
37
// because the static member _clipboard depends upon it
40
38
// for its own initialization
41
39
private static readonly PSConsoleReadLine _singleton ;
42
40
43
- private static readonly CancellationToken _defaultCancellationToken = new CancellationTokenSource ( ) . Token ;
44
-
45
41
// This is used by PowerShellEditorServices (the backend of the PowerShell VSCode extension)
46
42
// so that it can call PSReadLine from a delegate and not hit nested pipeline issues.
47
43
#pragma warning disable CS0649
@@ -143,17 +139,7 @@ private void ReadOneOrMoreKeys()
143
139
}
144
140
while ( _charMap . KeyAvailable )
145
141
{
146
- ConsoleKeyInfo keyInfo = _charMap . ReadKey ( ) ;
147
- if ( _cancelReadCancellationToken . IsCancellationRequested )
148
- {
149
- // If PSReadLine is running under a host that can cancel it, the
150
- // cancellation will come at a time when ReadKey is stuck waiting for input.
151
- // The next key press will be used to force it to return, and so we want to
152
- // discard this key since we were already canceled.
153
- continue ;
154
- }
155
-
156
- var key = PSKeyInfo . FromConsoleKeyInfo ( keyInfo ) ;
142
+ var key = PSKeyInfo . FromConsoleKeyInfo ( _charMap . ReadKey ( ) ) ;
157
143
_lastNKeys . Enqueue ( key ) ;
158
144
_queuedKeys . Enqueue ( key ) ;
159
145
}
@@ -170,10 +156,6 @@ private void ReadKeyThreadProc()
170
156
break ;
171
157
172
158
ReadOneOrMoreKeys ( ) ;
173
- if ( _cancelReadCancellationToken . IsCancellationRequested )
174
- {
175
- continue ;
176
- }
177
159
178
160
// One or more keys were read - let ReadKey know we're done.
179
161
_keyReadWaitHandle . Set ( ) ;
@@ -208,7 +190,6 @@ internal static PSKeyInfo ReadKey()
208
190
// - a key is pressed
209
191
// - the console is exiting
210
192
// - 300ms timeout - to process events if we're idle
211
- // - ReadLine cancellation is requested externally
212
193
handleId = WaitHandle . WaitAny ( _singleton . _requestKeyWaitHandles , 300 ) ;
213
194
if ( handleId != WaitHandle . WaitTimeout )
214
195
{
@@ -292,10 +273,12 @@ internal static PSKeyInfo ReadKey()
292
273
throw new OperationCanceledException ( ) ;
293
274
}
294
275
295
- if ( handleId == CancellationRequested )
276
+ if ( _singleton . _cancelReadCancellationToken . IsCancellationRequested )
296
277
{
297
- // ReadLine was cancelled. Save the current line to be restored next time ReadLine
298
- // is called, clear the buffer and throw an exception so we can return an empty string.
278
+ // ReadLine was cancelled. Dequeue the dummy input sent by the host, save the current
279
+ // line to be restored next time ReadLine is called, clear the buffer and throw an
280
+ // exception so we can return an empty string.
281
+ _singleton . _queuedKeys . Dequeue ( ) ;
299
282
_singleton . SaveCurrentLine ( ) ;
300
283
_singleton . _getNextHistoryIndex = _singleton . _history . Count ;
301
284
_singleton . _current = 0 ;
@@ -331,9 +314,7 @@ private void PrependQueuedKeys(PSKeyInfo key)
331
314
/// <returns>The complete command line.</returns>
332
315
public static string ReadLine ( Runspace runspace , EngineIntrinsics engineIntrinsics , bool ? lastRunStatus )
333
316
{
334
- // Use a default cancellation token instead of CancellationToken.None because the
335
- // WaitHandle is shared and could be triggered accidently.
336
- return ReadLine ( runspace , engineIntrinsics , _defaultCancellationToken , lastRunStatus ) ;
317
+ return ReadLine ( runspace , engineIntrinsics , CancellationToken . None , lastRunStatus ) ;
337
318
}
338
319
339
320
/// <summary>
@@ -396,7 +377,6 @@ public static string ReadLine(
396
377
}
397
378
398
379
_singleton . _cancelReadCancellationToken = cancellationToken ;
399
- _singleton . _requestKeyWaitHandles [ 2 ] = cancellationToken . WaitHandle ;
400
380
return _singleton . InputLoop ( ) ;
401
381
}
402
382
catch ( OperationCanceledException )
@@ -877,7 +857,7 @@ private void DelayedOneTimeInitialize()
877
857
_readKeyWaitHandle = new AutoResetEvent ( false ) ;
878
858
_keyReadWaitHandle = new AutoResetEvent ( false ) ;
879
859
_closingWaitHandle = new ManualResetEvent ( false ) ;
880
- _requestKeyWaitHandles = new WaitHandle [ ] { _keyReadWaitHandle , _closingWaitHandle , null } ;
860
+ _requestKeyWaitHandles = new WaitHandle [ ] { _keyReadWaitHandle , _closingWaitHandle } ;
881
861
_threadProcWaitHandles = new WaitHandle [ ] { _readKeyWaitHandle , _closingWaitHandle } ;
882
862
883
863
// This is for a "being hosted in an alternate appdomain scenario" (the
0 commit comments