From 7de225e9b2aa6d8c192df258845da7cb7f78bf28 Mon Sep 17 00:00:00 2001 From: Paul Betts Date: Thu, 16 Oct 2014 12:06:21 -0700 Subject: [PATCH] Always cancel on the UI thread. Fixes #96 --- src/ModernHttpClient/Android/OkHttpNetworkHandler.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs b/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs index 8a4fd62..7a48c69 100644 --- a/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs +++ b/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs @@ -11,6 +11,7 @@ using Java.IO; using System.Security.Cryptography.X509Certificates; using System.Globalization; +using Android.OS; namespace ModernHttpClient { @@ -85,7 +86,9 @@ protected override async Task SendAsync(HttpRequestMessage var rq = builder.Build(); var call = client.NewCall(rq); - cancellationToken.Register(() => call.Cancel()); + + // NB: Even closing a socket must be done off the UI thread. Cray! + cancellationToken.Register(() => Task.Run(() => call.Cancel())); var resp = default(Response); try {