Repository navigation
Conversation
fireEvent() called onNext() or buffered the event inside the function passed to AtomicLong.updateAndGet(), which is re-run when a concurrent request(n) changes the counter, so an event could be delivered twice. Directly delivered events could also overtake buffered ones, onComplete was lost when the stream completed with buffered events, and the demand overflowed on repeated request(Long.MAX_VALUE). Events are now only delivered from a drain loop that never runs concurrently; fireEvent(), request(), complete() and fireError() update the state and drain. The demand is capped at Long.MAX_VALUE, and the buffer limit only drops events that have not been requested yet.
coheigea
requested changes
Dec 4, 2026
coheigea
left a comment
Contributor
There was a problem hiding this comment.
Thanks, the drain loop is the right approach.
A few points:
- If onNext throws, the subscription stops delivering anything (please fix). drain() has no try/finally, so wip is never decremented. Every later drain() returns at once: later events are only buffered and onComplete never arrives. A test confirmed it: after a throwing onNext, three more events plus complete() delivered nothing and no onComplete. Rule 2.13 makes this the subscriber's fault, but the old code failed noisily and this fails silently. Suggestion: catch the exception, cancel the subscription and log it.
- Memory with unbounded demand. After request(Long.MAX_VALUE), the buffer limit never applies. If the subscriber calls request() from its own thread, that thread takes over draining. The reader then only adds events to the buffer and no longer waits for the subscriber, so the buffer can grow without limit. That's how this pattern usually behaves, but it would be worth a comment.
- Minor: if events are still buffered after complete(), request(n <= 0) is ignored because of the !completed.get() check in fireError. Rule 3.9 says it must signal onError; checking terminated would fix it. This was already the case before the PR.
- Nit: the request(1) before polling COMPLETE in testConcurrentEventsAndRequests isn't needed, because completion doesn't need demand.
A subscriber throwing from onNext left the drain loop without decrementing its work counter, so the subscription silently stopped delivering. Subscriber signals are now guarded: the failure is logged and the subscription cancelled. request(n <= 0) after complete() with buffered events signals onError (rule 3.9), and the effect of unbounded demand on the buffer is documented.
Contributor
Author
|
Thanks, addressed in the second commit:
|
coheigea
requested changes
Dec 4, 2026
coheigea
left a comment
Contributor
There was a problem hiding this comment.
A few more things to change:
- Don't let a failed close turn a completed stream into an error. fireError now checks terminated instead of completed, and SsePublisher's IOException catch also catches a failing br2.close() after complete(). If events are still buffered, the subscriber gets onError instead of onComplete and the buffered events are dropped. Fix: give request(n <= 0) its own error path that skips the completed check, and keep !completed.get() in fireError. Add a test that buffers events, calls complete(), then fireError(...), then request(...), and expects the events followed by onComplete.
- Don't swallow Error in signal(). Catching Error also catches OutOfMemoryError and StackOverflowError. Catch only RuntimeException, or rethrow VirtualMachineError.
…Error fireError() again leaves a completed stream alone, so a failing close of the event stream after complete() no longer replaces onComplete and drops the buffered events; only an invalid request(n) signals onError behind buffered events (rule 3.9). Subscriber failures no longer swallow VirtualMachineErrors; the subscription is cancelled before the error is rethrown. A negative buffer size behaves like 0 again.
Contributor
Author
|
Thanks, addressed in the third commit:
While re-checking I also found that a negative buffer size ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://issues.apache.org/jira/browse/CXF-9290
SseSubscription(MicroProfile Rest Client SSE publisher) delivers events from the SSE reader thread while the subscriber requests them from another thread without coordinating the two:fireEvent()callsonNext()or buffers the event inside the function passed toAtomicLong.updateAndGet(), which is re-run when a concurrentrequest(n)changes the counter, so an event can be delivered twice.onCompleteis lost when the stream completes while events are buffered.request(Long.MAX_VALUE)overflows the demand (rule 3.17).This makes the MP Rest Client TCK test
ReactiveStreamsPublisherTckTest#required_createPublisher3MustProduceAStreamOfExactly3Elementsflaky ("Expected end-of-stream but got element").Events are now only delivered from a drain loop that never runs concurrently (work-in-progress counter).
fireEvent(),request(),complete()andfireError()update the state and drain, the terminal event is signalled exactly once after the buffer is empty, the demand is capped atLong.MAX_VALUE, and the buffer size limit only drops events that have not been requested yet.New tests in
SseSubscriptionTest: concurrent events and requests (the TCK scenario, 2000 iterations),onCompleteafter buffered events are requested, unbounded demand, and requested events withbufferSize=0. The first three fail without the change.cxf-rt-rs-mp-clientbuilds with all tests and checks.