Repository navigation
Add onDiscard callback to Flow.fromPublisher - #529
Merged
Merged
Conversation
…d FlowReactiveStreams
…hen onDiscard throws or a send is interrupted
…ancel a second subscription
This was referenced Nov 30, 2026
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.
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.
Flow.fromPublisherbuffers 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).fromPublisher(p, onDiscard: T => Unit), in core and inFlowReactiveStreams. Each element received from the publisher is either emitted, or passed toonDiscardexactly 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.ChannelClosedException.Error(cause). Buffered elements are not emitted after an error, they are discarded.onDiscardmay 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.onSubscribeis now cancelled (Reactive Streams rule 2.5).take, elements arriving after cancellation, a throwingonDiscard, an interrupted send, and a second subscription. Plus a stress test checking that each element is either emitted or discarded, never both or neither. Updateddoc/streaming/flows.md.Closes #528
🤖 Generated with Claude Code