ServerStream.stream()
should release underlying resources when calling Stream.close()
method
#3358
Labels
priority: p2
Moderately-important priority. Fix may not be included in next release.
type: bug
Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Is your feature request related to a problem? Please describe.
When I use
ServerStream.stream()
and call any Stream terminal operation that does not consume all elements of the stream likefindAny()
,anyMatch(Predicate<?>)
, all underlying resources of theServerStream
are not released.Describe the solution you'd like
The derived stream should close all underlying resources of the main
ServerStream
when it is closed.To do that, the
stream()
method should register anonClose(Runnable)
on the stream before returning it.This
Runnable
should cancel theServerStream
when the stream is not fully consumed.With this, the stream user should add the statement try-with-resources to ensure all underlying resources are released in all cases.
Proposal implementation steps
In
ServerStreamIterator
, a new method can be created to be able to know if the iterator is fully consumed.In
ServerStream
, a new method can be created to cancel only when iterator is not fully consumed.In
ServerStream
, thestream()
method can be updated to register theonClose(Runnable)
to call the above methodDescribe alternatives you've considered
I have tried two different workarounds.
Register a
onClose(Runnable)
that cancel theServerStream
But with this,
ServerStream
is canceled even if all its items have been successfully consumed. I don't think it's a good practice to do this.Not use
stream()
method, but create the stream from theiterator()
and register theonClose(Runnable)
on it that check result ofiterator.hasNext()
to cancel or not theServerStream
But
iterator.hasNext()
can force theThread
to wait until next element is received, which is a waste of time in that case.The text was updated successfully, but these errors were encountered: