Richard Zowalla created CXF-9290:
------------------------------------
Summary: SseSubscription delivers events twice, out of order or
loses onComplete when events and requests race
Key: CXF-9290
URL: https://issues.apache.org/jira/browse/CXF-9290
Project: CXF
Issue Type: Bug
Reporter: Richard Zowalla
{{org.apache.cxf.microprofile.client.sse.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\(\)}} calls {{subscriber.onNext\(\)}} or {{buffer\(\)}} inside the
function passed to {{delivered.updateAndGet\(...\)}}.
{{AtomicLong.updateAndGet}} re\-runs that function when its compare\-and\-set
fails, which happens whenever {{request\(n\)}} drains the buffer at the same
time. The event is then delivered and buffered, so the subscriber receives it
twice.
* An event delivered directly in {{fireEvent\(\)}} can overtake an event still
in the buffer, so events arrive out of order.
* {{complete\(\)}} only signals {{onComplete}} when the buffer is empty, and
{{request\(n\)}} never checks again after draining the buffer, so
{{onComplete}} is lost when the stream completes while events are buffered.
* {{requested.addAndGet\(n\)}} overflows: calling
{{request\(Long.MAX\_VALUE\)}} twice makes the demand negative and the stream
stalls \(reactive streams rule 3.17\).
This makes the MicroProfile Rest Client TCK test
{{ReactiveStreamsPublisherTckTest#required\_createPublisher3MustProduceAStreamOfExactly3Elements}}
flaky \("Expected end\-of\-stream but got element
\[...InboundSseEventImpl...\]"\). A harness that fires 3 events and
{{complete\(\)}} on one thread while another requests them one by one fails in
most iterations.
Proposed fix: deliver only from a serialized drain loop \(work\-in\-progress
counter\): {{fireEvent\(\)}} buffers and drains, {{request\(n\)}} adds demand
\(capped at {{Long.MAX\_VALUE}}\) and drains,
{{complete\(\)}}/{{fireError\(\)}} set the terminal state and drain, and the
drain loop signals the terminal event exactly once when the buffer is empty.
The buffer size limit only drops events nobody has requested yet.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)