Skip to content

Commit c269609

Browse files
author
Paul Hohensee
committed
8347995: Race condition in jdk/java/net/httpclient/offline/FixedResponseHttpClient.java
Backport-of: a62a870150cf199f16277b478af2f5d937255b3c
1 parent f153824 commit c269609

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, 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
@@ -189,8 +189,11 @@ public HttpClient.Version version() {
189189
if (obp.isPresent()) {
190190
ConsumingSubscriber subscriber = new ConsumingSubscriber();
191191
obp.get().subscribe(subscriber);
192+
// wait for our subscriber to be completed and get the
193+
// list of ByteBuffers it received.
194+
var buffers = subscriber.getBuffers().join();
192195
if (responseBodyBytes == ECHO_SENTINAL) {
193-
responseBody = subscriber.buffers;
196+
responseBody = buffers;
194197
}
195198
}
196199

@@ -226,6 +229,13 @@ public HttpClient.Version version() {
226229
*/
227230
private static class ConsumingSubscriber implements Flow.Subscriber<ByteBuffer> {
228231
final List<ByteBuffer> buffers = Collections.synchronizedList(new ArrayList<>());
232+
// A CompletableFuture that will be completed with a list of ByteBuffers that the
233+
// ConsumingSubscriber has consumed.
234+
final CompletableFuture<List<ByteBuffer>> consumed = new CompletableFuture<>();
235+
236+
public final CompletableFuture<List<ByteBuffer>> getBuffers() {
237+
return consumed;
238+
}
229239

230240
@Override
231241
public void onSubscribe(Flow.Subscription subscription) {
@@ -236,8 +246,8 @@ public void onSubscribe(Flow.Subscription subscription) {
236246
buffers.add(item.duplicate());
237247
}
238248

239-
@Override public void onError(Throwable throwable) { assert false : "Unexpected"; }
249+
@Override public void onError(Throwable throwable) { consumed.completeExceptionally(throwable); }
240250

241-
@Override public void onComplete() { /* do nothing */ }
251+
@Override public void onComplete() { consumed.complete(buffers.stream().toList()); }
242252
}
243253
}

0 commit comments

Comments
 (0)