Repository: grpc/grpc-java
Issue: OkHttp: race between sendCancel and sendFrame.

If sendCancel is called (by timeout for example) before the stream is started, a following sendFrame will cause a NPE:

```
java.lang.NullPointerException
    at io.grpc.okhttp.OkHttpClientStream.sendFrame(OkHttpClientStream.java:197)
    at io.grpc.internal.AbstractClientStream.internalSendFrame(AbstractClientStream.java:199)
    at io.grpc.internal.AbstractStream$2.deliverFrame(AbstractStream.java:128)
    at io.grpc.internal.MessageFramer.commitToSink(MessageFramer.java:297)
    at io.grpc.internal.MessageFramer.flush(MessageFramer.java:255)
    at io.grpc.internal.AbstractStream.flush(AbstractStream.java:178)
    at io.grpc.ClientCallImpl.sendMessage(ClientCallImpl.java:213)
    at io.grpc.stub.ClientCalls$CallToStreamObserverAdapter.onNext(ClientCalls.java:210)
    at io.grpc.testing.integration.AbstractTransportTest.timeoutOnSleepingServer(AbstractTransportTest.java:843)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.FailOnTimeout$StatementThread.run(FailOnTimeout.java:74)
```


Changed files:
okhttp/src/main/java/io/grpc/okhttp/OkHttpClientStream.java, okhttp/src/test/java/io/grpc/okhttp/OkHttpClientTransportTest.java

diff --git a/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientStream.java b/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientStream.java
index ffa4b316b..9a299b595 100644
--- a/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientStream.java
+++ b/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientStream.java
@@ -190,11 +190,14 @@ class OkHttpClientStream extends Http2ClientStream {
     }
 
     synchronized (lock) {
+      if (cancelSent) {
+        return;
+      }
       if (pendingData != null) {
         // Stream is pending start, queue the data.
         pendingData.add(new PendingData(buffer, endOfStream, flush));
       } else {
-        checkState(id() != 0, "streamId should be set");
+        checkState(id() != null, "streamId should be set");
         // If buffer > frameWriter.maxDataLength() the flow-controller will ensure that it is
         // properly chunked.
         outboundFlow.data(endOfStream, id(), buffer, flush);
diff --git a/okhttp/src/test/java/io/grpc/okhttp/OkHttpClientTransportTest.java b/okhttp/src/test/java/io/grpc/okhttp/OkHttpClientTransportTest.java
index cf81e9cce..997d4adf6 100644
--- a/okhttp/src/test/java/io/grpc/okhttp/OkHttpClientTransportTest.java
+++ b/okhttp/src/test/java/io/grpc/okhttp/OkHttpClientTransportTest.java
@@ -197,7 +197,7 @@ public class OkHttpClientTransportTest {
     startTransport(3, null, true, 1);
 
     MockStreamListener listener = new MockStreamListener();
-    clientTransport.newStream(method, new Metadata.Headers(), listener).request(1);
+    clientTransport.newStream(method, new Metadata(), listener).request(1);
     assertContainStream(3);
     frameHandler().headers(false, false, 3, 0, grpcResponseHeaders(), HeadersMode.HTTP_20_HEADERS);
     assertNotNull(listener.headers);