Fix NPE in FluxWindowTimeout when window is null #4104
+5
−0
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.
Add null check in WindowTimeoutWithBackpressureSubscriber.onNext() to handle hot publishers that emit before downstream request().
Elements are dropped gracefully using Operators.onNextDropped() when window hasn't been created yet, maintaining backpressure semantics.
Fix potential NullPointerException in FluxWindowTimeout when hot publishers emit before downstream requests
This change improves the robustness of
FluxWindowTimeoutby adding a defensive null check for thewindowfield in theonNext()method. While this scenario is rare and typically indicates upstream spec violation, the fix prevents application crashes and provides graceful degradation by safely dropping data usingOperators.onNextDropped().The issue occurs in
WindowTimeoutWithBackpressureSubscriber.onNext()wherethis.windowcan be null ifonNext()is called before the downstream subscriber callsrequest(). This can happen when:The
windowfield is initialized tonulland only gets assigned in thedrain()method, which is triggered by downstreamrequest()calls. If a spec-violating publisher callsonNext()before anyrequest(), accessingwindow.sendNext(t)results in NPE.Root cause analysis:
final InnerWindow<T> window = this.window;- can be nullif (window.sendNext(t))- NPE if window is nulldrain()method which is called fromrequest()request()is madeSolution:
Added null check before accessing
window.sendNext(t). If window is null, the data is safely dropped usingOperators.onNextDropped()with appropriate context, maintaining system stability while logging the dropped element for debugging.This follows Reactor's defensive programming principles and improves the operator's resilience against spec-violating upstream publishers, similar to other operators in the codebase that include such protective measures.