Avoid resume-suspend race condition
This commit turns suspendReading() into a readingPaused() notification that is invoked after a succession of reads stops because there is no more demand. Sub-classes can use this notification to suspend, if that applies to them. Most importantly the notification is guaranteed not to overlap with checkOnDataAvailable() which means that suspend does not need to be atomic and guarded against resume. The two can and do compete all the time when reading ends with no demand, and a request for demand arrives concurrently. Issue: SPR-16207
This commit is contained in:
@@ -116,9 +116,14 @@ public abstract class AbstractListenerReadPublisher<T> implements Publisher<T> {
|
||||
protected abstract T read() throws IOException;
|
||||
|
||||
/**
|
||||
* Suspend reading, if the underlying API provides such a mechanism.
|
||||
* Invoked when reading is paused due to a lack of demand.
|
||||
* <p><strong>Note:</strong> This method is guaranteed not to compete with
|
||||
* {@link #checkOnDataAvailable()} so it can be used to safely suspend
|
||||
* reading, if the underlying API supports it, i.e. without competing with
|
||||
* an implicit call to resume via {@code checkOnDataAvailable()}.
|
||||
* @since 5.0.2
|
||||
*/
|
||||
protected abstract void suspendReading();
|
||||
protected abstract void readingPaused();
|
||||
|
||||
|
||||
// Private methods for use in State...
|
||||
@@ -280,7 +285,7 @@ public abstract class AbstractListenerReadPublisher<T> implements Publisher<T> {
|
||||
if (Operators.validate(n)) {
|
||||
Operators.addCap(DEMAND_FIELD_UPDATER, publisher, n);
|
||||
// Did a concurrent read transition to NO_DEMAND just before us?
|
||||
if (publisher.changeState(NO_DEMAND, DEMAND)) {
|
||||
if (publisher.changeState(NO_DEMAND, this)) {
|
||||
publisher.checkOnDataAvailable();
|
||||
}
|
||||
}
|
||||
@@ -288,23 +293,6 @@ public abstract class AbstractListenerReadPublisher<T> implements Publisher<T> {
|
||||
|
||||
@Override
|
||||
<T> void onDataAvailable(AbstractListenerReadPublisher<T> publisher) {
|
||||
for (;;) {
|
||||
if (!read(publisher)) {
|
||||
return;
|
||||
}
|
||||
// Maybe demand arrived between readAndPublish and READING->NO_DEMAND?
|
||||
long r = publisher.demand;
|
||||
if (r == 0 || publisher.changeState(NO_DEMAND, this)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return whether to exit the read loop; false means stop trying
|
||||
* to read, true means check demand one more time.
|
||||
*/
|
||||
<T> boolean read(AbstractListenerReadPublisher<T> publisher) {
|
||||
if (publisher.changeState(this, READING)) {
|
||||
try {
|
||||
boolean demandAvailable = publisher.readAndPublish();
|
||||
@@ -313,18 +301,22 @@ public abstract class AbstractListenerReadPublisher<T> implements Publisher<T> {
|
||||
publisher.checkOnDataAvailable();
|
||||
}
|
||||
}
|
||||
else if (publisher.changeState(READING, NO_DEMAND)) {
|
||||
publisher.suspendReading();
|
||||
return true;
|
||||
else {
|
||||
publisher.readingPaused();
|
||||
if (publisher.changeState(READING, NO_DEMAND)) {
|
||||
// Demand may have arrived since readAndPublish returned
|
||||
long r = publisher.demand;
|
||||
if (r > 0 && publisher.changeState(NO_DEMAND, this)) {
|
||||
publisher.checkOnDataAvailable();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
publisher.onError(ex);
|
||||
}
|
||||
}
|
||||
// Either competing onDataAvailable calls (via request or container callback)
|
||||
// Or a concurrent completion
|
||||
return false;
|
||||
// Else, either competing onDataAvailable (request vs container), or concurrent completion
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -267,7 +267,7 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void suspendReading() {
|
||||
protected void readingPaused() {
|
||||
// no-op
|
||||
}
|
||||
|
||||
|
||||
@@ -150,30 +150,14 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
|
||||
|
||||
@Override
|
||||
protected void checkOnDataAvailable() {
|
||||
// TODO: The onDataAvailable() call below can cause a StackOverflowError
|
||||
// since this method is being called from onDataAvailable() itself.
|
||||
if (isReadPossible()) {
|
||||
onDataAvailable();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isReadPossible() {
|
||||
if (!this.channel.isReadResumed()) {
|
||||
this.channel.resumeReads();
|
||||
}
|
||||
return this.channel.isReadResumed();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void suspendReading() {
|
||||
this.channel.suspendReads();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAllDataRead() {
|
||||
this.channel.getReadSetter().set(null);
|
||||
this.channel.resumeReads();
|
||||
super.onAllDataRead();
|
||||
// We are allowed to try, it will return null if data is not available
|
||||
onDataAvailable();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void readingPaused() {
|
||||
this.channel.suspendReads();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user