Skip to content

Commit

Permalink
Release mutex when facing AbandonedMutexException (#2867)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxian-dbw authored Sep 20, 2021
1 parent 82e35b8 commit 118b9e5
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions PSReadLine/History.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,24 @@ private bool WithHistoryFileMutexDo(int timeout, Action action)
_historyFileMutex.ReleaseMutex();
}
}

// Consider it a failure if we timed out on the mutex.
return false;
}
catch (AbandonedMutexException)
{
retryCount += 1;

// We acquired the mutex object that was abandoned by another powershell process.
// Now, since we own it, we must release it before retry, otherwise, we will miss
// a release and keep holding the mutex, in which case the 'WaitOne' calls from
// all other powershell processes will time out.
_historyFileMutex.ReleaseMutex();
}
} while (retryCount > 0 && retryCount < 3);

// No errors to report, so consider it a success even if we timed out on the mutex.
return true;
// If we reach here, that means we've done the retries but always got the 'AbandonedMutexException'.
return false;
}

private void WriteHistoryRange(int start, int end, bool overwritten)
Expand Down Expand Up @@ -427,18 +436,16 @@ private bool MaybeReadHistoryFile()

private void ReadHistoryFile()
{
WithHistoryFileMutexDo(1000, () =>
if (File.Exists(Options.HistorySavePath))
{
if (!File.Exists(Options.HistorySavePath))
WithHistoryFileMutexDo(1000, () =>
{
return;
}

var historyLines = File.ReadAllLines(Options.HistorySavePath);
UpdateHistoryFromFile(historyLines, fromDifferentSession: false, fromInitialRead: true);
var fileInfo = new FileInfo(Options.HistorySavePath);
_historyFileLastSavedSize = fileInfo.Length;
});
var historyLines = File.ReadAllLines(Options.HistorySavePath);
UpdateHistoryFromFile(historyLines, fromDifferentSession: false, fromInitialRead: true);
var fileInfo = new FileInfo(Options.HistorySavePath);
_historyFileLastSavedSize = fileInfo.Length;
});
}
}

void UpdateHistoryFromFile(IEnumerable<string> historyLines, bool fromDifferentSession, bool fromInitialRead)
Expand Down

0 comments on commit 118b9e5

Please sign in to comment.