Fix incremental history saving when the history queue is full #1602
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Summary
Fix #1581
Before this change,
WriteHistoryRangereads the new content from the history file and populate the new items to the history queue before the writing.This is fine when the circular history queue is not full, but when the queue is full, new items will push the existing items in the queue forward, and thus the
starttoendrange will be pointing to different items in the queue. This is causing the bug reported in #1581The fix is to read the new content from history file before the writing, but defer populating the history queue until after the writing is done (no matter the writing failed or succeeded).
This change also avoids calling
WithHistoryFileMutexDowithin aWithHistoryFileMutexDo. The nestedWithHistoryFileMutexDocall causes themutexto be released before the outerWithHistoryFileMutexDofinishes. I'm not familiar with the recursive semantics ofMutex, so I'm not sure if this is a true problem, but I did experience mutex issues on Windows, where all PowerShell sessions failed to grab the mutex in the call_historyFileMutex.WaitOne(timeout), and all history lookup operations became super slow. I hope that can be fixed by avoiding nestedWithHistoryFileMutexDocalls.It may be easier to review by ignoring white space characters:
https://github.com/PowerShell/PSReadLine/pull/1602/files?w=1
PR Checklist
Microsoft Reviewers: Open in CodeFlow