Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync ReadKeyProc thread with pipeline thread #3294

Merged
merged 4 commits into from
Apr 27, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 7 additions & 23 deletions PSReadLine/ReadLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public partial class PSConsoleReadLine : IPSConsoleReadLineMockableMethods
{
private const int ConsoleExiting = 1;

private const int CancellationRequested = 2;

// *must* be initialized in the static ctor
// because the static member _clipboard depends upon it
// for its own initialization
Expand Down Expand Up @@ -143,17 +141,7 @@ private void ReadOneOrMoreKeys()
}
while (_charMap.KeyAvailable)
{
ConsoleKeyInfo keyInfo = _charMap.ReadKey();
if (_cancelReadCancellationToken.IsCancellationRequested)
{
// If PSReadLine is running under a host that can cancel it, the
// cancellation will come at a time when ReadKey is stuck waiting for input.
// The next key press will be used to force it to return, and so we want to
// discard this key since we were already canceled.
continue;
}

var key = PSKeyInfo.FromConsoleKeyInfo(keyInfo);
var key = PSKeyInfo.FromConsoleKeyInfo(_charMap.ReadKey());
_lastNKeys.Enqueue(key);
_queuedKeys.Enqueue(key);
}
Expand All @@ -170,10 +158,6 @@ private void ReadKeyThreadProc()
break;

ReadOneOrMoreKeys();
if (_cancelReadCancellationToken.IsCancellationRequested)
{
continue;
}

// One or more keys were read - let ReadKey know we're done.
_keyReadWaitHandle.Set();
Expand Down Expand Up @@ -208,7 +192,6 @@ internal static PSKeyInfo ReadKey()
// - a key is pressed
// - the console is exiting
// - 300ms timeout - to process events if we're idle
// - ReadLine cancellation is requested externally
handleId = WaitHandle.WaitAny(_singleton._requestKeyWaitHandles, 300);
if (handleId != WaitHandle.WaitTimeout)
{
Expand Down Expand Up @@ -292,10 +275,12 @@ internal static PSKeyInfo ReadKey()
throw new OperationCanceledException();
}

if (handleId == CancellationRequested)
if (_singleton._cancelReadCancellationToken.IsCancellationRequested)
{
// ReadLine was cancelled. Save the current line to be restored next time ReadLine
// is called, clear the buffer and throw an exception so we can return an empty string.
// ReadLine was cancelled. Dequeue the dummy input sent by the host, save the current
// line to be restored next time ReadLine is called, clear the buffer and throw an
// exception so we can return an empty string.
_singleton._queuedKeys.Dequeue();
_singleton.SaveCurrentLine();
_singleton._getNextHistoryIndex = _singleton._history.Count;
_singleton._current = 0;
Expand Down Expand Up @@ -396,7 +381,6 @@ public static string ReadLine(
}

_singleton._cancelReadCancellationToken = cancellationToken;
_singleton._requestKeyWaitHandles[2] = cancellationToken.WaitHandle;
return _singleton.InputLoop();
}
catch (OperationCanceledException)
Expand Down Expand Up @@ -877,7 +861,7 @@ private void DelayedOneTimeInitialize()
_readKeyWaitHandle = new AutoResetEvent(false);
_keyReadWaitHandle = new AutoResetEvent(false);
_closingWaitHandle = new ManualResetEvent(false);
_requestKeyWaitHandles = new WaitHandle[] {_keyReadWaitHandle, _closingWaitHandle, null};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ha, the questions I had earlier but hadn't written yet are now irrelevant. Yay!

_requestKeyWaitHandles = new WaitHandle[] {_keyReadWaitHandle, _closingWaitHandle};
_threadProcWaitHandles = new WaitHandle[] {_readKeyWaitHandle, _closingWaitHandle};

// This is for a "being hosted in an alternate appdomain scenario" (the
Expand Down