Skip to content

Commit 5c17070

Browse files
snowpjoeyb
authored andcommitted
server: set latestResponse before sending response (#71)
By setting the latest response prior to sending the response we guarantee that the map is updated by the time the corresponding request arrives. This ensures that the nonce check correctly uses the most recent response. Signed-off-by: Snow Pettersen <[email protected]>
1 parent 4622181 commit 5c17070

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

server/src/main/java/io/envoyproxy/controlplane/server/DiscoveryServer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public void onNext(DiscoveryRequest request) {
185185
ads,
186186
request,
187187
ackedResources.getOrDefault(typeUrl, Collections.emptySet()),
188-
r -> latestResponse.put(typeUrl, send(r, typeUrl)));
188+
r -> send(r, typeUrl));
189189
});
190190

191191
return;
@@ -223,7 +223,7 @@ private void cancel() {
223223
watches.values().forEach(Watch::cancel);
224224
}
225225

226-
private DiscoveryResponse send(Response response, String typeUrl) {
226+
private void send(Response response, String typeUrl) {
227227
String nonce = Long.toString(streamNonce.getAndIncrement());
228228

229229
DiscoveryResponse discoveryResponse = DiscoveryResponse.newBuilder()
@@ -237,9 +237,11 @@ private DiscoveryResponse send(Response response, String typeUrl) {
237237

238238
callbacks.onStreamResponse(streamId, response.request(), discoveryResponse);
239239

240+
// Store the latest response *before* we send the response. This ensures that by the time the request
241+
// is processed the map is guaranteed to be updated. Doing it afterwards leads to a race conditions
242+
// which may see the incoming request arrive before the map is updated, failing the nonce check erroneously.
243+
latestResponse.put(typeUrl, discoveryResponse);
240244
responseObserver.onNext(discoveryResponse);
241-
242-
return discoveryResponse;
243245
}
244246
};
245247
}

0 commit comments

Comments
 (0)