Skip to content
Merged
Changes from all 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
62 changes: 33 additions & 29 deletions Src/LiquidProjections.PollingEventStore/Subscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void Start()
exception);
}
},
cancellationTokenSource.Token,
CancellationToken.None,
TaskCreationOptions.DenyChildAttach | TaskCreationOptions.LongRunning,
TaskScheduler.Default)
.Unwrap();
Expand Down Expand Up @@ -168,43 +168,47 @@ public void Dispose()
{
isDisposed = true;

// Wait for the task asynchronously.
Task.Run(() =>
{
if (cancellationTokenSource != null)
{
#if DEBUG
LogProvider.GetLogger(typeof(Subscription)).Debug(() => $"Subscription {Id} is being stopped.");
LogProvider.GetLogger(typeof(Subscription)).Debug(() => $"Subscription {Id} is being stopped.");
#endif

if (!cancellationTokenSource.IsCancellationRequested)
{
cancellationTokenSource.Cancel();
}

try
{
Task?.Wait();
}
catch (AggregateException)
{
// Ignore.
}

cancellationTokenSource.Dispose();
if (cancellationTokenSource != null)
{
try
{
cancellationTokenSource.Cancel();
}

lock (eventStoreAdapter.subscriptionLock)
catch (AggregateException)
{
eventStoreAdapter.subscriptions.Remove(this);
// Ignore.
}
}

#if DEBUG
LogProvider.GetLogger(typeof(Subscription)).Debug(() => $"Subscription {Id} has been stopped.");
#endif
});
lock (eventStoreAdapter.subscriptionLock)
{
eventStoreAdapter.subscriptions.Remove(this);
}

if (Task == null)
{
FinishDisposing();
}
else
{
// Wait for the task asynchronously.
Task.ContinueWith(_ => FinishDisposing());
}
}
}
}

private void FinishDisposing()
{
cancellationTokenSource?.Dispose();

#if DEBUG
LogProvider.GetLogger(typeof(Subscription)).Debug(() => $"Subscription {Id} has been stopped.");
#endif
}
}
}