Don't await a null task if connecting errors earlier than expected.

This commit is contained in:
RogueException
2015-10-01 13:49:12 -03:00
parent 8458465b71
commit 9ff53b9393
2 changed files with 12 additions and 12 deletions

View File

@@ -156,7 +156,6 @@ namespace Discord
} }
catch catch
{ {
await Disconnect().ConfigureAwait(false); await Disconnect().ConfigureAwait(false);
throw; throw;
} }
@@ -170,19 +169,19 @@ namespace Discord
/// <summary> Disconnects from the Discord server, canceling any pending requests. </summary> /// <summary> Disconnects from the Discord server, canceling any pending requests. </summary>
public Task Disconnect() => DisconnectInternal(new Exception("Disconnect was requested by user."), isUnexpected: false); public Task Disconnect() => DisconnectInternal(new Exception("Disconnect was requested by user."), isUnexpected: false);
protected Task DisconnectInternal(Exception ex = null, bool isUnexpected = true, bool skipAwait = false) protected async Task DisconnectInternal(Exception ex = null, bool isUnexpected = true, bool skipAwait = false)
{ {
int oldState; int oldState;
bool hasWriterLock; bool hasWriterLock;
//If in either connecting or connected state, get a lock by being the first to switch to disconnecting //If in either connecting or connected state, get a lock by being the first to switch to disconnecting
oldState = Interlocked.CompareExchange(ref _state, (int)DiscordClientState.Disconnecting, (int)DiscordClientState.Connecting); oldState = Interlocked.CompareExchange(ref _state, (int)DiscordClientState.Disconnecting, (int)DiscordClientState.Connecting);
if (oldState == (int)DiscordClientState.Disconnected) return TaskHelper.CompletedTask; //Already disconnected if (oldState == (int)DiscordClientState.Disconnected) return; //Already disconnected
hasWriterLock = oldState == (int)DiscordClientState.Connecting; //Caused state change hasWriterLock = oldState == (int)DiscordClientState.Connecting; //Caused state change
if (!hasWriterLock) if (!hasWriterLock)
{ {
oldState = Interlocked.CompareExchange(ref _state, (int)DiscordClientState.Disconnecting, (int)DiscordClientState.Connected); oldState = Interlocked.CompareExchange(ref _state, (int)DiscordClientState.Disconnecting, (int)DiscordClientState.Connected);
if (oldState == (int)DiscordClientState.Disconnected) return TaskHelper.CompletedTask; //Already disconnected if (oldState == (int)DiscordClientState.Disconnected) return; //Already disconnected
hasWriterLock = oldState == (int)DiscordClientState.Connected; //Caused state change hasWriterLock = oldState == (int)DiscordClientState.Connected; //Caused state change
} }
@@ -192,14 +191,16 @@ namespace Discord
_disconnectReason = ex != null ? ExceptionDispatchInfo.Capture(ex) : null; _disconnectReason = ex != null ? ExceptionDispatchInfo.Capture(ex) : null;
_cancelTokenSource.Cancel(); _cancelTokenSource.Cancel();
/*if (_state == DiscordClientState.Connecting) //_runTask was never made /*if (_disconnectState == DiscordClientState.Connecting) //_runTask was never made
await Cleanup().ConfigureAwait(false);*/ await Cleanup().ConfigureAwait(false);*/
} }
if (!skipAwait) if (!skipAwait)
return _runTask ?? TaskHelper.CompletedTask; {
else Task task = _runTask;
return TaskHelper.CompletedTask; if (_runTask != null)
await task.ConfigureAwait(false);
}
} }
private async Task RunTasks() private async Task RunTasks()

View File

@@ -138,11 +138,10 @@ namespace Discord.WebSockets
if (!skipAwait) if (!skipAwait)
{ {
Task task = _runTask ?? TaskHelper.CompletedTask; Task task = _runTask;
await task; if (_runTask != null)
await task.ConfigureAwait(false);
} }
else
await TaskHelper.CompletedTask;
} }
protected virtual async Task RunTasks() protected virtual async Task RunTasks()