Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ public synchronized SessionStream newStream(
return new SessionStreamImpl(innerCall) {
// null until onBeforeSessionStart fires
@Nullable AfeId afeId = null;
boolean sessionEstablished = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The sessionEstablished field is read outside of a synchronized block in onMessage (line 332) but written inside a synchronized block (line 334). To ensure proper memory visibility and prevent thread-local caching or instruction reordering under the Java Memory Model (JMM), this field should be declared volatile.

Suggested change
boolean sessionEstablished = false;
volatile boolean sessionEstablished = false;
References
  1. Ensure that fields used for cross-thread state checks, such as exceptions set by background threads and read by application threads, are declared as volatile to guarantee memory visibility and establish a proper happens-before relationship under the Java Memory Model.


@Override
public void start(Listener responseListener, Metadata headers) {
Expand All @@ -317,8 +318,6 @@ public void start(Listener responseListener, Metadata headers) {
public void onBeforeSessionStart(PeerInfo peerInfo) {
afeId = AfeId.extract(peerInfo);
synchronized (ChannelPoolDpImpl.this) {
fw.consecutiveFailures = 0;
recycleBackoff = INITIAL_RECYCLE_BACKOFF;
sessionsPerAfeId.add(afeId);
if (!fw.channel.isShutdown()) {
routeObservations.put(new RouteKey(fw.id, tenant), afeId);
Expand All @@ -328,13 +327,32 @@ public void onBeforeSessionStart(PeerInfo peerInfo) {
super.onBeforeSessionStart(peerInfo);
}

@Override
public void onMessage(SessionResponse msg) {
if (!sessionEstablished && msg.hasOpenSession()) {
synchronized (ChannelPoolDpImpl.this) {
sessionEstablished = true;
fw.consecutiveFailures = 0;
recycleBackoff = INITIAL_RECYCLE_BACKOFF;
}
}
super.onMessage(msg);
}

@Override
public void onClose(Status status, Metadata trailers) {
synchronized (ChannelPoolDpImpl.this) {
if (afeId != null) {
sessionsPerAfeId.remove(afeId);
} else if (!status.isOk() && status.getCode() != Code.CANCELLED) {
}
if (status.isOk()) {
fw.consecutiveFailures = 0;
recycleBackoff = INITIAL_RECYCLE_BACKOFF;
} else if (!sessionEstablished && status.getCode() != Code.CANCELLED) {
fw.consecutiveFailures++;
if (afeId != null) {
routeObservations.remove(new RouteKey(fw.id, tenant), afeId);
}
}
releaseChannel(fw, status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,35 @@ void testRecycleChannelBackoff() {

pool.close();
}

@Test
void testPreOpenSessionFailureWithPeerInfoIncrementsFailures() {
when(channelSupplier.get()).thenReturn(channel);
when(channel.newCall(any(), any())).thenReturn(clientCall);
doNothing().when(clientCall).start(listener.capture(), any());
doReturn(Attributes.EMPTY).when(clientCall).getAttributes();

ChannelPoolDpImpl pool =
new ChannelPoolDpImpl(channelSupplier, defaultConfig, debugTagTracer, bgExecutor);

PeerInfo peerInfo = PeerInfo.newBuilder().setApplicationFrontendId(555).build();
Metadata headers = new Metadata();
headers.put(
SessionStreamImpl.PEER_INFO_KEY,
Base64.getEncoder().encodeToString(peerInfo.toByteArray()));

// 5 pre-OpenSessionResponse rejections (onHeaders with PeerInfo followed by onClose(!OK)
// without OpenSessionResponse) must count as consecutive failures and recycle the channel.
for (int i = 0; i < 5; i++) {
pool.newStream(FakeSessionGrpc.getOpenSessionMethod(), CallOptions.DEFAULT)
.start(mock(Listener.class), new Metadata());
listener.getValue().onHeaders(headers);
listener.getValue().onClose(Status.RESOURCE_EXHAUSTED, new Metadata());
}

verify(channel, times(1)).shutdown();
verify(channelSupplier, times(2)).get();

pool.close();
}
}
Loading