spring-framework: SSE emitter completion callback not called after network error in 3.1.3
In Spring Boot 3.1.3, the completion callback does not get called when an SSE emitter completes with a network error. This works as expected in Spring Boot 3.1.2.
In the reproducer below, I’ve added a call to SseEmitter::completeWithError in an (unsuccessful) attempt to work around this issue in 3.1.3. This call is not needed in 3.1.2.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>my-repro</groupId>
<artifactId>application</artifactId>
<version>0.1</version>
<properties>
<java.version>20</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<release>${java.version}</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
</project>
ReproTest.java
package repro;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.time.Duration;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = ReproTest.class)
@SpringBootApplication
public class ReproTest {
record WebListener(SseEmitter sseEmitter) {
static WebListener create() {
return new WebListener(new SseEmitter(0L));
}
}
static class EventSource {
private final Set<WebListener> listeners = new HashSet<>();
void addListener(WebListener listener) {
System.out.println("Adding listener");
listeners.add(listener);
listener.sseEmitter().onCompletion(() -> {
System.out.println("Removing listener");
listeners.remove(listener);
});
}
Set<WebListener> listeners() {
return Set.copyOf(listeners);
}
@Scheduled(fixedRate = 2, timeUnit = TimeUnit.SECONDS)
void sendEvents() {
for (WebListener listener : listeners) {
try {
listener.sseEmitter().send(SseEmitter.event().name("PING"));
} catch (IOException | RuntimeException e) {
try {
listener.sseEmitter().completeWithError(e);
} catch (Exception ex) {
ex.printStackTrace();
}
e.printStackTrace();
}
}
}
}
@EnableScheduling
@Configuration
static class Config {
@Bean
EventSource eventSource() {
return new EventSource();
}
}
@RestController
static class Controller {
@Autowired
private EventSource eventSource;
@GetMapping("/stream")
public SseEmitter streamEvents() {
WebListener listener = WebListener.create();
eventSource.addListener(listener);
return listener.sseEmitter();
}
}
@LocalServerPort
int port;
@Autowired
EventSource eventSource;
@Test
void listener_should_be_removed_after_network_error() throws IOException, InterruptedException {
try (Socket socket = new Socket()) {
socket.setReceiveBufferSize(1);
socket.connect(new InetSocketAddress("localhost", port));
try (var writer = new OutputStreamWriter(socket.getOutputStream())) {
writer.write("""
GET /stream
Accept: text/event-stream
""");
writer.flush();
}
Thread.sleep(1_000);
} // socket.close()
Thread.sleep(20_000);
assert eventSource.listeners().isEmpty();
}
}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (4 by maintainers)
We are also seeing this issue after upgrading to Spring Boot 3.1.3 and therefor its upgrade to Tomcat 10.1.12. We didn’t notice the actual missing of the error handler, but these log messages:
@Ironlink I have replayed your test case and can see exactly the same log messages.
After overriding/pinning the dependency to Tomcat to 10.1.11 manually (the version included in SB 3.1.2 which works), the test will run fine again, no log messages anymore.
Speaking of our environment (just for the records):
After playing with downgrading the different parties, I would rather go this is a bug in Tomcat 10.1.12+ and not in Spring or Spring Boot. Maybe someone else can confirm this?
Sorry, I forgot to update this issue. The fix for this will be included in the next version of Tomcat, 10.1.17.
https://bz.apache.org/bugzilla/show_bug.cgi?id=68227
See https://bz.apache.org/bugzilla/show_bug.cgi?id=67235
I have the same issue with Spring Boot 3.1.3 and Tomcat. Downgrade to 3.1.2 fixes the problem.
The NullPointerException occurs when the SSE connection is closed in the browser using EventSource.close() Completion Callback is never executed.