Skip to content

Add onDiscard callback to Flows.fromPublisher - #419

Merged
adamw merged 10 commits into
mainfrom
port-ox-frompublisher-discard
Dec 1, 2026
Merged

adamw merged 10 commits into
mainfrom
port-ox-frompublisher-discard

Conversation

@adamw

@adamw adamw commented Nov 30, 2026

Copy link
Copy Markdown
Member

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.

@adamw
adamw merged commit 983bb49 into main Dec 1, 2026
15 of 16 checks passed
@adamw
adamw deleted the port-ox-frompublisher-discard branch December 1, 2026 07:56
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.

1 participant