diff --git a/src/Discord.Net/DiscordSimpleClient.cs b/src/Discord.Net/DiscordSimpleClient.cs index 745735cae..71d2f26c4 100644 --- a/src/Discord.Net/DiscordSimpleClient.cs +++ b/src/Discord.Net/DiscordSimpleClient.cs @@ -156,7 +156,6 @@ namespace Discord } catch { - await Disconnect().ConfigureAwait(false); throw; } @@ -170,19 +169,19 @@ namespace Discord /// Disconnects from the Discord server, canceling any pending requests. 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; bool hasWriterLock; //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); - 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 if (!hasWriterLock) { 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 } @@ -192,14 +191,16 @@ namespace Discord _disconnectReason = ex != null ? ExceptionDispatchInfo.Capture(ex) : null; _cancelTokenSource.Cancel(); - /*if (_state == DiscordClientState.Connecting) //_runTask was never made + /*if (_disconnectState == DiscordClientState.Connecting) //_runTask was never made await Cleanup().ConfigureAwait(false);*/ } if (!skipAwait) - return _runTask ?? TaskHelper.CompletedTask; - else - return TaskHelper.CompletedTask; + { + Task task = _runTask; + if (_runTask != null) + await task.ConfigureAwait(false); + } } private async Task RunTasks() diff --git a/src/Discord.Net/WebSockets/WebSocket.cs b/src/Discord.Net/WebSockets/WebSocket.cs index 3d64e9263..79537a3cf 100644 --- a/src/Discord.Net/WebSockets/WebSocket.cs +++ b/src/Discord.Net/WebSockets/WebSocket.cs @@ -138,11 +138,10 @@ namespace Discord.WebSockets 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()