title: SocketException on cancel call using OkHttp
body: <p>The following code represents downloading a file. The call will be canceled when the Observable is disposed. In my case the Observable will be disposed in the Fragment's onDestroy() method (due to the fact it will not need to continue downloading the file on a Fragment that is or will be destroyed). Whenever a scenario takes place of cancelation during the downloading process of the file, this code will crash with a SocketException. The message can either be a "Socket Closed" or a "Socket operation on non-socket". My question is how to properly cancel an ongoing download request using OkHttp?</p>
  
  <p>The code:</p>
  
  <pre><code>Request request = //..
  final Call call = client.newCall(request);
  
     call.enqueue(new Callback() {
  
         @Override
         public void onFailure(Call call, IOException error)
         {
            Log.e("Acme", "OnFailure:", error);
            emitter.onError(error);
         }
  
         @Override
         public void onResponse(Call call, Response response) throws IOException
         {
             if (response.isSuccessful())
             {
                 BufferedSink sink = null;
                 BufferedSource source = null;
                 try
                 {
                     Log.d("Acme", "onResponse: Start writing..");
                     sink = Okio.buffer(Okio.sink(file));
                     source = response.body().source();
                     sink.writeAll(source); // Crash on this line.
                     sink.flush();
  
                     emitter.onNext(file);
                     emitter.onComplete();
                 }
                 catch (IOException exception)
                 {
                     Log.e("Acme", "onResponse: Exception writing file:", exception);
                     emitter.onError(exception);
                 }
                 finally
                 {
                     Log.d("Acme", "onResponse: Trying to close..");
                     Util.closeQuietly(sink);
                     Util.closeQuietly(source);
                     Log.d("Acme", "onResponse: Closed..");
                 }
             }
         }
     });
  
     emitter.setCancellable(new Cancellable() {
  
         @Override
         public void cancel() throws Exception 
         {
             if (!call.isCanceled()) 
             {
                 call.cancel();
             }
         }
     });
  }
  </code></pre>
  
  <p>Console:</p>
  
  <pre><code>D/Acme: onResponse: Start writing..
  E/AndroidRuntime: FATAL EXCEPTION: OkHttp https://acme.nl/...
  Process: nl.acme.app.development.debug, PID: 12432
  java.net.SocketException: Socket operation on non-socket
      at java.net.SocketInputStream.socketRead0(Native Method)
      at java.net.SocketInputStream.read(SocketInputStream.java:151)
      at java.net.SocketInputStream.read(SocketInputStream.java:120)
      at okio.Okio$2.read(Okio.java:138)
      at okio.AsyncTimeout$2.read(AsyncTimeout.java:236)
      at okio.RealBufferedSource.read(RealBufferedSource.java:45)
      at okhttp3.internal.http1.Http1Codec$FixedLengthSource.read(Http1Codec.java:385)
      at okio.RealBufferedSource.read(RealBufferedSource.java:45)
      at okio.RealBufferedSink.writeAll(RealBufferedSink.java:97)
      at nl.app.acme.infrastructure.network.Api$44$1.onResponse(Api.java:3322)
      at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
      at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
      at java.lang.Thread.run(Thread.java:761)
  </code></pre>