8297132: BACKOUT JDK-8296889: Race condition when cancelling a request

Reviewed-by: djelinski, alanb
This commit is contained in:
Daniel Fuchs 2022-11-16 13:13:54 +00:00
parent e72b0ac4af
commit 8b1ff9e37e
4 changed files with 6 additions and 25 deletions
src/java.net.http/share/classes/jdk/internal/net/http
test/jdk/java/net/httpclient

@ -208,11 +208,6 @@ class Http1Exchange<T> extends ExchangeImpl<T> {
this.exchange = exchange;
}
@Override
public void onSubscribed() {
exchange.registerResponseSubscriber(this);
}
@Override
protected void complete(Throwable t) {
try {
@ -464,6 +459,7 @@ class Http1Exchange<T> extends ExchangeImpl<T> {
BodySubscriber<T> subscriber = handler.apply(response);
Http1ResponseBodySubscriber<T> bs =
new Http1ResponseBodySubscriber<T>(subscriber, this);
registerResponseSubscriber(bs);
return bs;
}

@ -344,6 +344,7 @@ class Stream<T> extends ExchangeImpl<T> {
Http2StreamResponseSubscriber<T> createResponseSubscriber(BodyHandler<T> handler, ResponseInfo response) {
Http2StreamResponseSubscriber<T> subscriber =
new Http2StreamResponseSubscriber<>(handler.apply(response));
registerResponseSubscriber(subscriber);
return subscriber;
}
@ -1542,22 +1543,17 @@ class Stream<T> extends ExchangeImpl<T> {
super(subscriber);
}
@Override
public void onSubscribed() {
registerResponseSubscriber(this);
}
@Override
protected void complete(Throwable t) {
try {
unregisterResponseSubscriber(this);
Stream.this.unregisterResponseSubscriber(this);
} finally {
super.complete(t);
}
}
@Override
protected void onCancel() {
unregisterResponseSubscriber(this);
Stream.this.unregisterResponseSubscriber(this);
}
}

@ -127,15 +127,6 @@ public class HttpBodySubscriberWrapper<T> implements TrustedSubscriber<T> {
*/
protected void onCancel() { }
/**
* Called right after the userSubscriber::onSubscribe is called.
* @apiNote
* This method may be used by subclasses to perform cleanup
* related actions after a subscription has been succesfully
* accepted.
*/
protected void onSubscribed() { }
/**
* Complete the subscriber, either normally or exceptionally
* ensure that the subscriber is completed only once.
@ -178,9 +169,8 @@ public class HttpBodySubscriberWrapper<T> implements TrustedSubscriber<T> {
public void onSubscribe(Flow.Subscription subscription) {
// race condition with propagateError: we need to wait until
// subscription is finished before calling onError;
boolean onSubscribed;
synchronized (this) {
if ((onSubscribed = subscribed.compareAndSet(false, true))) {
if (subscribed.compareAndSet(false, true)) {
SubscriptionWrapper wrapped = new SubscriptionWrapper(subscription);
userSubscriber.onSubscribe(this.subscription = wrapped);
} else {
@ -191,7 +181,6 @@ public class HttpBodySubscriberWrapper<T> implements TrustedSubscriber<T> {
assert completed.get();
}
}
if (onSubscribed) onSubscribed();
}
@Override

@ -23,7 +23,7 @@
/*
* @test
* @bug 8245462 8229822 8254786 8296889
* @bug 8245462 8229822 8254786
* @summary Tests cancelling the request.
* @library /test/lib http2/server
* @key randomness