diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index d6798248a..d87b61aff 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -62,7 +62,7 @@ namespace Discord.Commands await replyChannel.SendMessage("Unable to display help: Unknown command.").ConfigureAwait(false); } else //Show general help - await ShowGeneralHelp(e.User, e.Channel, replyChannel); + await ShowGeneralHelp(e.User, e.Channel, replyChannel).ConfigureAwait(false); }); } diff --git a/src/Discord.Net/DiscordClient.cs b/src/Discord.Net/DiscordClient.cs index 443497ca5..282606031 100644 --- a/src/Discord.Net/DiscordClient.cs +++ b/src/Discord.Net/DiscordClient.cs @@ -180,7 +180,7 @@ namespace Discord { try { - using (await _connectionLock.LockAsync()) + using (await _connectionLock.LockAsync().ConfigureAwait(false)) { if (State != ConnectionState.Disconnected) await Disconnect().ConfigureAwait(false); @@ -218,7 +218,7 @@ namespace Discord } catch (Exception ex) { - await _taskManager.SignalError(ex); + await _taskManager.SignalError(ex).ConfigureAwait(false); throw; } } @@ -268,19 +268,20 @@ namespace Discord } /// Disconnects from the Discord server, canceling any pending requests. - public async Task Disconnect() - { - if (State == ConnectionState.Connected) - await ClientAPI.Send(new LogoutRequest()).ConfigureAwait(false); - await _taskManager.Stop(true).ConfigureAwait(false); - } + public Task Disconnect() => _taskManager.Stop(true); private async Task Cleanup() { + var oldState = State; State = ConnectionState.Disconnecting; + + if (oldState == ConnectionState.Connected) + await ClientAPI.Send(new LogoutRequest()).ConfigureAwait(false); + if (Config.UseMessageQueue) MessageQueue.Clear(); - await GatewaySocket.Disconnect(); + + await GatewaySocket.Disconnect().ConfigureAwait(false); ClientAPI.Token = null; GatewaySocket.Token = null; GatewaySocket.SessionId = null; diff --git a/src/Discord.Net/MessageQueue.cs b/src/Discord.Net/MessageQueue.cs index 4152c3059..e4321b365 100644 --- a/src/Discord.Net/MessageQueue.cs +++ b/src/Discord.Net/MessageQueue.cs @@ -116,7 +116,7 @@ namespace Discord.Net IQueuedAction queuedAction; while (_pendingActions.TryDequeue(out queuedAction)) - await queuedAction.Do(this); + await queuedAction.Do(this).ConfigureAwait(false); await Task.Delay(interval).ConfigureAwait(false); } diff --git a/src/Discord.Net/Models/Channel.cs b/src/Discord.Net/Models/Channel.cs index 05a382874..b64fce45f 100644 --- a/src/Discord.Net/Models/Channel.cs +++ b/src/Discord.Net/Models/Channel.cs @@ -366,7 +366,7 @@ namespace Discord public async Task SendFile(string filePath) { using (var stream = File.OpenRead(filePath)) - return await SendFile(Path.GetFileName(filePath), stream); + return await SendFile(Path.GetFileName(filePath), stream).ConfigureAwait(false); } public async Task SendFile(string filename, Stream stream) { diff --git a/src/Discord.Net/Net/Rest/BuiltInEngine.cs b/src/Discord.Net/Net/Rest/BuiltInEngine.cs index 7856eb234..cb37d6648 100644 --- a/src/Discord.Net/Net/Rest/BuiltInEngine.cs +++ b/src/Discord.Net/Net/Rest/BuiltInEngine.cs @@ -58,7 +58,7 @@ namespace Discord.Net.Rest { if (json != null) request.Content = new StringContent(json, Encoding.UTF8, "application/json"); - return await Send(request, cancelToken); + return await Send(request, cancelToken).ConfigureAwait(false); } } public async Task SendFile(string method, string path, string filename, Stream stream, CancellationToken cancelToken) @@ -68,7 +68,7 @@ namespace Discord.Net.Rest var content = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture)); content.Add(new StreamContent(File.OpenRead(path)), "file", filename); request.Content = content; - return await Send(request, cancelToken); + return await Send(request, cancelToken).ConfigureAwait(false); } } private async Task Send(HttpRequestMessage request, CancellationToken cancelToken) @@ -103,7 +103,7 @@ namespace Discord.Net.Rest var now = DateTime.UtcNow; if (now >= _rateLimitTime) { - using (await _rateLimitLock.LockAsync()) + using (await _rateLimitLock.LockAsync().ConfigureAwait(false)) { if (now >= _rateLimitTime) { @@ -120,7 +120,7 @@ namespace Discord.Net.Rest else if (statusCode < 200 || statusCode >= 300) //2xx = Success throw new HttpException(response.StatusCode); else - return await response.Content.ReadAsStringAsync(); + return await response.Content.ReadAsStringAsync().ConfigureAwait(false); } } diff --git a/src/Discord.Net/Net/Rest/RestClient.cs b/src/Discord.Net/Net/Rest/RestClient.cs index c7814c638..727182037 100644 --- a/src/Discord.Net/Net/Rest/RestClient.cs +++ b/src/Discord.Net/Net/Rest/RestClient.cs @@ -130,7 +130,7 @@ namespace Discord.Net.Rest if (request == null) throw new ArgumentNullException(nameof(request)); OnSendingRequest(request); - var results = await SendFile(request, false); + var results = await SendFile(request, false).ConfigureAwait(false); OnSentRequest(request, null, null, results.Milliseconds); } diff --git a/src/Discord.Net/Net/Rest/SharpRestEngine.cs b/src/Discord.Net/Net/Rest/SharpRestEngine.cs index fce8633a5..8ec300c11 100644 --- a/src/Discord.Net/Net/Rest/SharpRestEngine.cs +++ b/src/Discord.Net/Net/Rest/SharpRestEngine.cs @@ -92,7 +92,7 @@ namespace Discord.Net.Rest var now = DateTime.UtcNow; if (now >= _rateLimitTime) { - using (await _rateLimitLock.LockAsync()) + using (await _rateLimitLock.LockAsync().ConfigureAwait(false)) { if (now >= _rateLimitTime) { diff --git a/src/Discord.Net/Net/WebSockets/WS4NetEngine.cs b/src/Discord.Net/Net/WebSockets/WS4NetEngine.cs index 916d90e40..785e4f813 100644 --- a/src/Discord.Net/Net/WebSockets/WS4NetEngine.cs +++ b/src/Discord.Net/Net/WebSockets/WS4NetEngine.cs @@ -88,7 +88,7 @@ namespace Discord.Net.WebSockets private async void OnWebSocketError(object sender, ErrorEventArgs e) { - await _taskManager.SignalError(e.Exception); + await _taskManager.SignalError(e.Exception).ConfigureAwait(false); _waitUntilConnect.Set(); _waitUntilDisconnect.Set(); } @@ -99,7 +99,7 @@ namespace Discord.Net.WebSockets ex = new WebSocketException((e as ClosedEventArgs).Code, (e as ClosedEventArgs).Reason); else ex = new Exception("Connection lost"); - await _taskManager.SignalError(ex); + await _taskManager.SignalError(ex).ConfigureAwait(false); _waitUntilConnect.Set(); _waitUntilDisconnect.Set(); } diff --git a/src/Discord.Net/Net/WebSockets/WebSocket.cs b/src/Discord.Net/Net/WebSockets/WebSocket.cs index 6cf103f72..accf31a07 100644 --- a/src/Discord.Net/Net/WebSockets/WebSocket.cs +++ b/src/Discord.Net/Net/WebSockets/WebSocket.cs @@ -73,7 +73,7 @@ namespace Discord.Net.WebSockets { try { - using (await _lock.LockAsync()) + using (await _lock.LockAsync().ConfigureAwait(false)) { await _taskManager.Stop().ConfigureAwait(false); _taskManager.ClearException(); @@ -90,7 +90,7 @@ namespace Discord.Net.WebSockets catch (Exception ex) { //TODO: Should this be inside the lock? - await _taskManager.SignalError(ex); + await _taskManager.SignalError(ex).ConfigureAwait(false); throw; } } @@ -106,7 +106,7 @@ namespace Discord.Net.WebSockets } catch (Exception ex) { - await _taskManager.SignalError(ex); + await _taskManager.SignalError(ex).ConfigureAwait(false); } } diff --git a/src/Discord.Net/TaskManager.cs b/src/Discord.Net/TaskManager.cs index affd9b225..4934e53d9 100644 --- a/src/Discord.Net/TaskManager.cs +++ b/src/Discord.Net/TaskManager.cs @@ -46,7 +46,7 @@ namespace Discord if (task != null) await Stop().ConfigureAwait(false); - using (await _lock.LockAsync()) + using (await _lock.LockAsync().ConfigureAwait(false)) { _cancelSource = cancelSource; @@ -67,14 +67,14 @@ namespace Discord //Signal the rest of the tasks to stop if (firstTask.Exception != null) - await SignalError(firstTask.Exception); + await SignalError(firstTask.Exception).ConfigureAwait(false); else - await SignalStop(); + await SignalStop().ConfigureAwait(false); //Wait for the other tasks, and signal their errors too just in case try { await allTasks.ConfigureAwait(false); } - catch (AggregateException ex) { await SignalError(ex.InnerExceptions.First()); } - catch (Exception ex) { await SignalError(ex); } + catch (AggregateException ex) { await SignalError(ex.InnerExceptions.First()).ConfigureAwait(false); } + catch (Exception ex) { await SignalError(ex).ConfigureAwait(false); } //Run the cleanup function within our lock if (_stopAction != null) @@ -89,7 +89,7 @@ namespace Discord public async Task SignalStop(bool isExpected = false) { - using (await _lock.LockAsync()) + using (await _lock.LockAsync().ConfigureAwait(false)) { if (isExpected) _wasStopExpected = true; @@ -104,7 +104,7 @@ namespace Discord public async Task Stop(bool isExpected = false) { Task task; - using (await _lock.LockAsync()) + using (await _lock.LockAsync().ConfigureAwait(false)) { if (isExpected) _wasStopExpected = true; @@ -116,12 +116,12 @@ namespace Discord if (!_cancelSource.IsCancellationRequested && _cancelSource != null) _cancelSource.Cancel(); } - await task; + await task.ConfigureAwait(false); } public async Task SignalError(Exception ex) { - using (await _lock.LockAsync()) + using (await _lock.LockAsync().ConfigureAwait(false)) { if (_stopReason != null) return; @@ -133,7 +133,7 @@ namespace Discord public async Task Error(Exception ex) { Task task; - using (await _lock.LockAsync()) + using (await _lock.LockAsync().ConfigureAwait(false)) { if (_stopReason != null) return; @@ -146,7 +146,7 @@ namespace Discord _cancelSource.Cancel(); } } - await task; + await task.ConfigureAwait(false); } /// Throws an exception if one was captured.