Skip to content

Add onDiscard callback to Flow.fromPublisher - #529

Merged
adamw merged 16 commits into
masterfrom
implement-ox-issue-528-94aa2fb6
Nov 30, 2026
Merged

adamw merged 16 commits into
masterfrom
implement-ox-issue-528-94aa2fb6

Conversation

@adamw

@adamw adamw commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Flow.fromPublisher buffers elements in a channel. Some of them could be received from the publisher but never emitted, and nothing released them. That leaks elements which hold resources, such as Netty's ref-counted ByteBufs. Tapir currently tracks these elements itself (softwaremill/tapir#5573).

  • New overload fromPublisher(p, onDiscard: T => Unit), in core and in FlowReactiveStreams. Each element received from the publisher is either emitted, or passed to onDiscard exactly once. Elements are discarded when the publisher or the flow fails, is interrupted, or stops early (e.g. take), and also when they arrive after the subscription is cancelled.
  • Publisher errors still fail fast, and are still thrown as ChannelClosedException.Error(cause). Buffered elements are not emitted after an error, they are discarded.
  • onDiscard may run at the same time on the publisher's thread and the flow's thread, so it must be thread-safe. If it throws while the flow ends, the remaining elements are still discarded, and the exception is added as suppressed to the one that ended the flow.
  • A second onSubscribe is now cancelled (Reactive Streams rule 2.5).
  • The existing one-argument overloads pass a no-op callback. The new method is an overload, not a default parameter, so binary compatibility is kept.
  • Tests for publisher error, consumer exception, interruption, take, elements arriving after cancellation, a throwing onDiscard, an interrupted send, and a second subscription. Plus a stress test checking that each element is either emitted or discarded, never both or neither. Updated doc/streaming/flows.md.

Closes #528

🤖 Generated with Claude Code

@adamw
adamw merged commit 2bd06c1 into master Nov 30, 2026
11 of 12 checks passed
@adamw
adamw deleted the implement-ox-issue-528-94aa2fb6 branch November 30, 2026 15:58
adamw added a commit that referenced this pull request Nov 30, 2026
`ChannelTest` "select a receive until all channels are done" failed on
master CI (JVM, Java 21, capacity 10000): it got 20 values and then
`Done`, instead of 100 values and then `Done`.

Cause: `selectOrClosed` returns `Done` as soon as any channel is done.
Each sender sends 10 values, sleeps 10ms, then marks its channel done.
If the receiver is slowed down for more than 10ms (e.g. a busy CI
machine), the first channel is done before the other channels are
drained, so the select returns `Done` early. The test is old and not
related to #529.

Fix: the test now only selects from channels that are not yet done, and
stops once all of them are done. It still checks that all 100 values are
received.

A 50ms pause in the receiver after 20 values reproduces the exact CI
failure with the old test. The new test passes with it.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
adamw added a commit to softwaremill/tapir that referenced this pull request Nov 30, 2026
…Discard, remove UnconsumedContentTracker (#5705)

Ox 1.1.2 (softwaremill/ox#529) adds
`FlowReactiveStreams.fromPublisher(publisher, onDiscard)`. Every element
the flow gets from the publisher is either emitted, or passed to
`onDiscard`. This PR bumps ox to 1.1.2 and passes `_.release()` as
`onDiscard` in the Netty sync server's multipart body reader. With that,
`UnconsumedContentTracker` and its lock, added in
#5573, are no longer needed, so they are removed.

Ox releases the chunks it doesn't emit. Our code releases the chunks it
does emit. `decodeChunk` already releases every chunk it gets. The only
chunk that was not released is the one that goes over `maxBytes`: ox
emits it, but it never reaches the decoder. It is now released before
`StreamMaxLengthExceededException` is thrown.

Also in this PR:
- `NettySyncRequestBodyTest` checks the ref counts of these chunks: the
chunk over `maxBytes`, a chunk that arrives after cancellation, and
chunks buffered when the publisher fails.
- `Http4sServerTest` has a formatting-only change.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

## Open review findings

What the run's review left open, each with the reason recorded —
including findings the fixer declined:

- Ensure the response assertion runs for every body result
(`server/tests/src/main/scala/sttp/tapir/server/tests/ServerBasicTests.scala:962`)
— Out of scope: ServerBasicTests.scala:962 (inputStreamTests) is
existing code that this change (ox 1.1.2 bump, tracker removal in the
Netty sync multipart body) does not touch.
adamw added a commit to softwaremill/jox that referenced this pull request Dec 1, 2026
Ports softwaremill/ox#529 to jox.

`fromPublisher` buffers elements in a channel. Elements received from
the publisher but never emitted were dropped, so nothing released them.
This leaks elements that hold resources, such as Netty's
reference-counted ByteBufs. The new overload
`Flows.fromPublisher(publisher, onDiscard)` passes each such element to
`onDiscard`. Every received element is either emitted or discarded
exactly once. The existing overload calls the new one with a no-op
callback, so it works as before.

Elements are discarded when:
- the publisher fails
- downstream throws or stops early (e.g. `take`)
- the flow is interrupted
- they arrive after the subscription is cancelled

Publisher errors still fail fast with the same exception
(`ChannelErrorException` caused by the publisher's error). `onDiscard`
may run on the publisher's threads and on the flow's thread at the same
time, so it must be thread-safe. If it throws while buffered elements
are being discarded, the rest are still discarded, and its exception is
added as suppressed to the exception that ended the flow. A second
subscription, or one that arrives after cancellation, is cancelled
straight away.

Also adds unit tests and a stress test, and updates `docs/flows.md`. The
otel modules and `ForkDecoratorTest` have formatting-only changes from
running spotless.

## Open review findings

What the run's review left open, each with the reason recorded —
including findings the fixer declined:

- Build fails under the installed JDK — Environmental: the default JDK
is 26. The build passes with JDK 25 (mise install), which is what the
project targets: `mvn -pl flows -am verify` reports 786 tests, 0
failures, BUILD SUCCESS.
- Build uses an incompatible JDK for preview features
(`structured/pom.xml`) — lint still failing after the fix turn
- Build blocked by unsupported Java preview release
(`structured/pom.xml`) — Environmental: the default JDK on this machine
is 26, while the project targets Java 25 with preview features. With JDK
25 (mise install), `mvn --batch-mode --update-snapshots verify` reports
BUILD SUCCESS. No project change needed.
- Build uses incompatible Java release and preview settings — lint still
failing after the fix turn
- Build requires unsupported Java preview release (`structured/pom.xml`)
— Environmental: the default JDK on PATH is 26, and the project targets
25 on purpose. The build passes with JAVA_HOME set to the installed JDK
25 (mise): flows module verify gives 1088 tests, 0 failures, BUILD
SUCCESS.
- Build uses an incompatible JDK preview release (`structured/pom.xml`)
— Environmental. The project targets JDK 25 with preview features on
purpose, and the reviewer's build ran on JDK 26. JDK 25 is installed
(mise) and the build passes with JAVA_HOME pointing at it: the flows
module verify (with -am) reported BUILD SUCCESS. Changing the configured
release is out of scope.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flow.fromPublisher: allow releasing elements which are buffered but not emitted

1 participant