Skip to content

Commit 77d8da6

Browse files
kieran-farrellcoffeys
authored andcommitted
8347995: Race condition in jdk/java/net/httpclient/offline/FixedResponseHttpClient.java
Backport-of: a62a870150cf199f16277b478af2f5d937255b3c
1 parent 96da000 commit 77d8da6

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

test/jdk/java/net/httpclient/offline/FixedResponseHttpClient.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -209,8 +209,11 @@ public HttpClient.Version version() {
209209
if (obp.isPresent()) {
210210
ConsumingSubscriber subscriber = new ConsumingSubscriber();
211211
obp.get().subscribe(subscriber);
212+
// wait for our subscriber to be completed and get the
213+
// list of ByteBuffers it received.
214+
var buffers = subscriber.getBuffers().join();
212215
if (responseBodyBytes == ECHO_SENTINAL) {
213-
responseBody = subscriber.buffers;
216+
responseBody = buffers;
214217
}
215218
}
216219

@@ -246,6 +249,13 @@ public HttpClient.Version version() {
246249
*/
247250
private static class ConsumingSubscriber implements Flow.Subscriber<ByteBuffer> {
248251
final List<ByteBuffer> buffers = Collections.synchronizedList(new ArrayList<>());
252+
// A CompletableFuture that will be completed with a list of ByteBuffers that the
253+
// ConsumingSubscriber has consumed.
254+
final CompletableFuture<List<ByteBuffer>> consumed = new CompletableFuture<>();
255+
256+
public final CompletableFuture<List<ByteBuffer>> getBuffers() {
257+
return consumed;
258+
}
249259

250260
@Override
251261
public void onSubscribe(Flow.Subscription subscription) {
@@ -256,9 +266,9 @@ public void onSubscribe(Flow.Subscription subscription) {
256266
buffers.add(item.duplicate());
257267
}
258268

259-
@Override public void onError(Throwable throwable) { assert false : "Unexpected"; }
269+
@Override public void onError(Throwable throwable) { consumed.completeExceptionally(throwable); }
260270

261-
@Override public void onComplete() { /* do nothing */ }
271+
@Override public void onComplete() { consumed.complete(buffers.stream().toList()); }
262272
}
263273

264274
@Override

0 commit comments

Comments
 (0)