Skip to content
Open
Show file tree
Hide file tree
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
46 changes: 42 additions & 4 deletions Microsoft.Azure.Cosmos/src/direct/Dispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,12 @@ private void StartIdleTimer()
}
}

private void OnIdleTimer(Task precedentTask)
private async Task OnIdleTimerAsync(Task precedentTask)
{
DefaultTrace.TraceInformation(
"[RNTBD Dispatcher {0}][{1}] Idle timer fired.",
this.ConnectionCorrelationId, this);

Task receiveTaskCopy = null;

Debug.Assert(!Monitor.IsEntered(this.connectionLock));
Expand Down Expand Up @@ -572,15 +576,25 @@ private void OnIdleTimer(Task precedentTask)
receiveTaskCopy = this.CloseConnection();
}

this.WaitTask(receiveTaskCopy, "receive loop");
await this.WaitTaskAsync(receiveTaskCopy, "receive loop")
.ConfigureAwait(false);
}

// this.connectionLock must be held.
private void ScheduleIdleTimer(TimeSpan timeToIdle)
{
Debug.Assert(Monitor.IsEntered(this.connectionLock));
this.idleTimer = this.idleTimerPool.GetPooledTimer((int)timeToIdle.TotalSeconds);
this.idleTimerTask = this.idleTimer.StartTimerAsync().ContinueWith(this.OnIdleTimer, TaskContinuationOptions.OnlyOnRanToCompletion);
// IMPORTANT: .Unwrap() is essential here. Without it, idleTimerTask
// would be Task<Task> and would complete when OnIdleTimerAsync
// returns its inner Task (at the first await), not when it
// finishes. StopIdleTimer() waits on idleTimerTask during
// shutdown; if idleTimerTask completes early, shutdown proceeds
// while OnIdleTimerAsync is still running, causing
// use-after-dispose on the connection. Do not remove .Unwrap().
this.idleTimerTask = this.idleTimer.StartTimerAsync()
.ContinueWith(this.OnIdleTimerAsync, TaskContinuationOptions.OnlyOnRanToCompletion)
.Unwrap();
this.idleTimerTask.ContinueWith(
failedTask =>
{
Expand Down Expand Up @@ -681,6 +695,30 @@ private void WaitTask(Task t, string description)
}
}

private async Task WaitTaskAsync(Task t, string description)
{
if (t == null)
{
return;
}
try
{
Debug.Assert(!Monitor.IsEntered(this.callLock));
Debug.Assert(!Monitor.IsEntered(this.connectionLock));
await t.ConfigureAwait(false);
}
catch (Exception e)
{
DefaultTrace.TraceWarning(
"[RNTBD Dispatcher {0}][{1}] Parallel task failed: {2}. " +
"Exception: {3}: {4}",
this.ConnectionCorrelationId, this, description,
e.GetType().Name, e.Message);
// Intentionally swallowing the exception. The caller can't
// do anything useful with it.
}
}

private void ThrowIfDisposed()
{
if (this.disposed)
Expand Down Expand Up @@ -1244,4 +1282,4 @@ private enum State
}
}
}
}
}
Loading
Loading