problem with resource leak warning from stream

50 Views Asked by At

I have the following code causing a Potential resource leak: '<unssigned Closable value>' may not be closed warning in static code check:

Stream<? extends someClass> stream = someCollection.stream()
    .limit(item -> !Thread.currentThread().isInterrupted()) //
    .limit(more limit criteria) //
    .peek(container) //
    .map(item -> new someClass(r, container.value()));

_publisher = new StreamPublisher<>();
_publisher.subscribe(subscriber);
_publisher.attach(stream);

I already tried to wrap the stream into a try-with-resource, but this does not resolve the issue:

try (Stream<? extends someClass> stream = someCollection.stream()
    .limit(item -> !Thread.currentThread().isInterrupted()) //
    .limit(more limit criteria) //
    .peek(container) //
    .map(item -> new someClass(r, container.value()))) {

        _publisher = new StreamPublisher<>();
        _publisher.subscribe(subscriber);
        _publisher.attach(stream);
    }

Any idea how to get rid of the warning?

1

There are 1 best solutions below

0
Shmuel Greenberger On

It may be that it's giving the warning because you're not short-circuiting the stream, although it does sound quite unlikely. Perhaps the _publisher.subscriber() isn't short circuiting?