reactor / reactor-core

Non-Blocking Reactive Foundation for the JVM

Home Page:http://projectreactor.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`FluxBuffer` hangs when buffer Supplier returns a Set

Sage-Pierce opened this issue · comments

If Flux.buffer(int maxSize, Supplier<Collection> bufferSupplier) is invoked where the bufferSupplier returns a Set, the stream may hang on duplicate upstream emissions and bounded downstream request.

Extra Context

I have a use case where I'd like to process batches of n distinct elements from a source at a time, and using Flux.buffer(n, LinkedHashSet::new) would be a convenient, concise way to do so.

Expected Behavior

The stream should not hang and should complete successfully

Actual Behavior

The stream hangs and never completes

Steps to Reproduce

This test will fail

	@Test
	public void supplierUsesSet() {
		Flux.just(1, 1, 2)
			.<Set<Integer>>buffer(2, HashSet::new)
			.take(1, true)
			.as(StepVerifier::create)
			.expectNext(Stream.of(1, 2).collect(Collectors.toSet()))
			.expectComplete()
			.verify(Duration.ofSeconds(2));
	}

If the test is changed to not contain duplicates, i.e. Flux.just(1, 2), the test passes.

Possible Solution

FluxBuffer ignores whether adding to the buffer actually has any effect on the buffer. If adding to the buffer doesn't modify it, an extra s.request(1) should be issued.

Your Environment

  • Reactor version(s) used: 3.7.0-SNAPSHOT
  • Other relevant libraries versions (eg. netty, ...): N/A
  • JVM version (java -version): 17.0.8
  • OS and version (eg uname -a): MacOS Darwin 23.5.0