Repository: square/okhttp
Issue: Crash with Facebook SDK

When I set `URL.setURLStreamHandlerFactory(new OkUrlFactory(client));` accordingly to https://github.com/square/picasso/issues/749, my app crashes because of the Facebook SDK : 

```
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.squareup.okhttp.internal.spdy.SpdyStream.close(com.squareup.okhttp.internal.spdy.ErrorCode)' on a null object reference
            at com.squareup.okhttp.internal.http.SpdyTransport.disconnect(SpdyTransport.java:221)
            at com.squareup.okhttp.internal.http.HttpEngine.disconnect(HttpEngine.java:465)
            at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.disconnect(HttpURLConnectionImpl.java:113)
            at com.squareup.okhttp.internal.huc.DelegatingHttpsURLConnection.disconnect(DelegatingHttpsURLConnection.java:93)
            at com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl.disconnect(HttpsURLConnectionImpl.java:25)
            at com.facebook.internal.Utility.disconnectQuietly(Utility.java:464)
            at com.facebook.Request.executeConnectionAndWait(Request.java:1563)
            at com.facebook.Request.executeBatchAndWait(Request.java:1460)
            at com.facebook.Request.executeBatchAndWait(Request.java:1429)
            at com.facebook.Request.executeBatchAndWait(Request.java:1411)
            at com.facebook.Request.executeAndWait(Request.java:1383)
            at com.facebook.Request.executeAndWait(Request.java:1269)
            at com.facebook.internal.Utility.getAppSettingsQueryResponse(Utility.java:708)
            at com.facebook.internal.Utility.access$000(Utility.java:57)
            at com.facebook.internal.Utility$1.doInBackground(Utility.java:630)
            at com.facebook.internal.Utility$1.doInBackground(Utility.java:627)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
```

Doesn't happen 100% of the time. Reproduced on Nexus 5 with Android 5.0 (LRX21O)


Changed files:
okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java
okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/HttpOverSpdyTest.java

diff --git a/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/HttpOverSpdyTest.java b/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/HttpOverSpdyTest.java
index 2c275e028..670361c60 100644
--- a/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/HttpOverSpdyTest.java
+++ b/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/HttpOverSpdyTest.java
@@ -16,6 +16,7 @@
 package com.squareup.okhttp.internal.http;
 
 import com.squareup.okhttp.Cache;
+import com.squareup.okhttp.ConnectionPool;
 import com.squareup.okhttp.OkHttpClient;
 import com.squareup.okhttp.OkUrlFactory;
 import com.squareup.okhttp.Protocol;
@@ -426,6 +427,26 @@ public abstract class HttpOverSpdyTest {
     assertContains(requestB.getHeaders(), "cookie: c=oreo");
   }
 
+  /** https://github.com/square/okhttp/issues/1191 */
+  @Test public void disconnectWithStreamNotEstablished() throws Exception {
+    ConnectionPool connectionPool = new ConnectionPool(5, 5000);
+    client.client().setConnectionPool(connectionPool);
+
+    server.enqueue(new MockResponse().setBody("abc"));
+    server.play();
+
+    // Disconnect before the stream is created. A connection is still established!
+    HttpURLConnection connection1 = client.open(server.getUrl("/"));
+    connection1.connect();
+    connection1.disconnect();
+
+    // That connection is pooled, and it works.
+    assertEquals(1, connectionPool.getSpdyConnectionCount());
+    HttpURLConnection connection2 = client.open(server.getUrl("/"));
+    assertContent("abc", connection2, 3);
+    assertEquals(0, server.takeRequest().getSequenceNumber());
+  }
+
   <T> void assertContains(Collection<T> collection, T value) {
     assertTrue(collection.toString(), collection.contains(value));
   }
diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java b/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java
index 0561ac38e..61b661081 100644
--- a/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java
+++ b/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java
@@ -212,7 +212,7 @@ public final class SpdyTransport implements Transport {
   }
 
   @Override public void disconnect(HttpEngine engine) throws IOException {
-    stream.close(ErrorCode.CANCEL);
+    if (stream != null) stream.close(ErrorCode.CANCEL);
   }
 
   @Override public boolean canReuseConnection() {