Skip to content

Commit 91ec839

Browse files
committed
testing: Fix race condition in MockServerTransportListener and improve stream cleanup in AbstractTransportTest
In MockServerTransportListener.streamCreated(), stream.setListener(listener) was called after streams.add(StreamCreation(...)). This created a race condition where a test thread calling takeStreamOrFail() could dequeue the stream and call serverStream.triggerEvent() before stream.setListener() was called by the transport/container thread. When this occurred (e.g. in TomcatTransportTest on multi-core runners), ServletServerStream invoked transportState.triggerEvent() on the test thread, saw a null listener, threw a NullPointerException (swallowed by SerializingExecutor), and never enqueued the event into the listener queue, leading to a timeout and assertion failure: expected:<...Object@...> but was:<null> Setting stream.setListener(listener) before enqueuing to streams guarantees that any thread consuming the StreamCreation will always observe a fully initialized listener. Additionally, in AbstractTransportTest.serverStream_triggerEvent(), replace clientStream.cancel(Status.CANCELLED) with serverStream.close(Status.OK, ...) for clean stream closure instead of leaving an uncoordinated client RST_STREAM in flight during container tearDown. TAG=agy CONV=e1bfa5a2-e855-4f79-abdd-ef2b264977be
1 parent b63b4a1 commit 91ec839

2 files changed

Lines changed: 2 additions & 2 deletions

File tree

core/src/testFixtures/java/io/grpc/internal/AbstractTransportTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2114,7 +2114,7 @@ public void serverStream_triggerEvent() throws Exception {
21142114
assertEquals(event, receivedEvent);
21152115

21162116
// Cleanup
2117-
clientStream.cancel(Status.CANCELLED);
2117+
serverStream.close(Status.OK, new Metadata());
21182118
}
21192119

21202120
@Test

core/src/testFixtures/java/io/grpc/internal/MockServerTransportListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public MockServerTransportListener(ServerTransport transport) {
4545
@Override
4646
public void streamCreated(ServerStream stream, String method, Metadata headers) {
4747
ServerStreamListenerBase listener = new ServerStreamListenerBase();
48-
streams.add(new StreamCreation(stream, method, headers, listener));
4948
stream.setListener(listener);
49+
streams.add(new StreamCreation(stream, method, headers, listener));
5050
}
5151

5252
@Override

0 commit comments

Comments
 (0)