Skip to content

Commit

Permalink
making sure that task will not leak to another thread
Browse files Browse the repository at this point in the history
  • Loading branch information
pictos committed Jun 10, 2024
1 parent f8c9c4f commit 8ae5af3
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions LiteDB/Engine/Disk/DiskWriterQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ 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, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
_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().ConfigureAwait(false);
_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 8ae5af3

Please sign in to comment.