Description
Summary
There is an inconsistency in the invocation behaviour of the SseEmitter.onCompletion()
callback between Spring Framework versions 6.2.3 and 6.2.5. Previously, calling emitter.complete()
would immediately trigger the callback registered via onCompletion()
. However, starting with Spring Framework 6.2.5 (used in Spring Boot 3.4), this callback no longer appears to execute after complete()
method is invoqued.
Current Behaviour
When using Spring Boot 3.4 (with Spring Framework 6.2.5), calling emitter.complete()
does not cause an immediate invocation of the onCompletion()
callback. This behaviour differs from that in Spring Boot 3.3 (with Spring Framework 6.2.3), where emitter.complete()
immediately triggered the callback.
Expected behaviour
The expected behaviour is that calling emitter.complete()
should trigger the onCompletion()
callback immediately, just as it did in Spring Boot 3.3 (Spring Framework 6.2.3).
Steps to Reproduce
You can reproduce the issue with the following setup:
- Implement the controller below in your application:
@RestController public class SseController { @GetMapping("/sse") public SseEmitter handleSse() { SseEmitter emitter = new SseEmitter(); emitter.onCompletion(() -> { System.out.println("onCompletion callback executed"); }); ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); scheduler.schedule(() -> { emitter.complete(); }, 1, TimeUnit.SECONDS); return emitter; } }
- Run the application with Spring Boot 3.3 (which uses Spring Framework 6.2.3) and observe that the log shows "onCompletion callback executed" immediately after calling
complete()
. - Update the application to Spring Boot 3.4 (with Spring Framework 6.2.5) and notice that the callback is not executed immediately.
Additional Notes
-
The code to reproduce this behaviour is available in the [spring-sse-emitter GitHub repository https://github.com/jrpedrianes/spring-sse-emitter.
-
This behavioural change appears to be related to 503 status code after completing SseEmitter in onTimeout #34426 in the Spring Framework repository.