Browse Source

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

tags/docs-0.9
RogueException 9 years ago
parent
commit
9ff53b9393
2 changed files with 12 additions and 12 deletions
  1. +9
    -8
      src/Discord.Net/DiscordSimpleClient.cs
  2. +3
    -4
      src/Discord.Net/WebSockets/WebSocket.cs

+ 9
- 8
src/Discord.Net/DiscordSimpleClient.cs 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
return TaskHelper.CompletedTask;
{
Task task = _runTask;
if (_runTask != null)
await task.ConfigureAwait(false);
}
} }


private async Task RunTasks() private async Task RunTasks()


+ 3
- 4
src/Discord.Net/WebSockets/WebSocket.cs View File

@@ -138,11 +138,10 @@ namespace Discord.WebSockets


if (!skipAwait) if (!skipAwait)
{ {
Task task = _runTask ?? TaskHelper.CompletedTask;
await task;
Task task = _runTask;
if (_runTask != null)
await task.ConfigureAwait(false);
} }
else
await TaskHelper.CompletedTask;
} }


protected virtual async Task RunTasks() protected virtual async Task RunTasks()


Loading…
Cancel
Save