Skip to content

Commit

Permalink
Merge pull request litedb-org#2447 from alexbereznikov/2446-fix-disk-…
Browse files Browse the repository at this point in the history
…writer-queue-context

Don't capture synchronization context in DiskWriterQueue
  • Loading branch information
pictos authored Jun 12, 2024
2 parents daf2c2c + 8ae5af3 commit e9cff85
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions LiteDB/Engine/Disk/DiskWriterQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public void EnqueuePage(PageBuffer page)
// throw last exception that stop running queue
if (_exception != null) throw _exception;

_queueIsEmpty.Reset();
_queue.Enqueue(page);
_queueHasItems.Set();

lock (_queueSync)
{
_queueIsEmpty.Reset();
_queue.Enqueue(page);
_queueHasItems.Set();

if (_task == null)
{
_task = Task.Factory.StartNew(ExecuteQueue, TaskCreationOptions.LongRunning);
Expand All @@ -76,7 +76,7 @@ public void Wait()
/// <summary>
/// Execute all items in queue sync
/// </summary>
private async Task ExecuteQueue()
private void ExecuteQueue()
{
try
{
Expand All @@ -88,19 +88,17 @@ private async Task ExecuteQueue()
}
else
{
lock (_queueSync)
{
if (_queue.Count > 0) continue;

_queueIsEmpty.Set();
_queueHasItems.Reset();
if (_queue.Count > 0) continue;

_queueIsEmpty.Set();
_queueHasItems.Reset();

if (_shouldClose) return;
}
if (_shouldClose) return;

_stream.FlushToDisk();

await _queueHasItems.WaitAsync();
_queueHasItems.WaitAsync().GetAwaiter().GetResult();
}
}
}
Expand Down Expand Up @@ -137,7 +135,7 @@ public void Dispose()
_shouldClose = true;
_queueHasItems.Set(); // unblock the running loop in case there are no items

_task?.Wait();
_task?.GetAwaiter().GetResult();
_task = null;
}
}
Expand Down

0 comments on commit e9cff85

Please sign in to comment.