Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The filter of Flux occasionally fails to take effect #3974

Closed
killersteps opened this issue Jan 24, 2025 · 1 comment
Closed

The filter of Flux occasionally fails to take effect #3974

killersteps opened this issue Jan 24, 2025 · 1 comment
Labels
for/stackoverflow Questions are best asked on SO or Gitter status/invalid We don't feel this issue is valid, or the root cause was found outside of Reactor

Comments

@killersteps
Copy link

When I use

Flux.interval(Duration.ofSeconds(1)).filter(tick -> {
  log.info("tick: {}", tick);
  return status;
}).take(1).flatMap(tick -> foo()).subscribe(); 

to perform a periodic check task, I expect foo() to be triggered when status=true. However, first of all, I must explain that this is occasional. When the content of log.info("tick: {}", tick); is not printed in the log, the foo() method is unexpectedly called, which means that the filter method occasionally fails, which makes me very distressed.
About the definition of the status variable:

private boolean status;

I tried to locate and reproduce the problem stably by myself, but unfortunately failed. I don’t think this is a concurrency and thread safety issue because no matter how the status changes, when the stream meets the conditions, the relevant logs should be printed, and I need to use status in foo(), but it is false, which means that the only reason that can explain it seems to be that the filter method fails?

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>3.4.1</version>
  <relativePath/>
</parent>

<dependency>
  <groupId>io.projectreactor</groupId>
  <artifactId>reactor-core</artifactId>
  <version>3.7.1</version>
  <scope>compile</scope>
</dependency>

<java.version>17</java.version>
@chemicL
Copy link
Member

chemicL commented Jan 24, 2025

Hi, @killersteps !

private boolean status; 

This declaration means that there will be publication issues in concurrent scenarios. interval operator uses a different Thread for publishing its events so it might not see the updated value of that field. Consider combining a functional approach here or at least make sure publishing happens via the volatile keyword.

Since this looks more like a question and not a bug, please consider Gitter or Stackoverflow for future questions.

@chemicL chemicL closed this as not planned Won't fix, can't repro, duplicate, stale Jan 24, 2025
@chemicL chemicL added status/invalid We don't feel this issue is valid, or the root cause was found outside of Reactor for/stackoverflow Questions are best asked on SO or Gitter labels Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for/stackoverflow Questions are best asked on SO or Gitter status/invalid We don't feel this issue is valid, or the root cause was found outside of Reactor
Projects
None yet
Development

No branches or pull requests

2 participants