Skip to content

Commit e988d48

Browse files
authored
fix: service destroy listener exceptions (#20622)
Related to #20577
1 parent 675a0a9 commit e988d48

3 files changed

Lines changed: 93 additions & 5 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/VaadinService.java‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,8 +2205,21 @@ public Registration addServiceDestroyListener(
22052205
*/
22062206
public void destroy() {
22072207
ServiceDestroyEvent event = new ServiceDestroyEvent(this);
2208-
serviceDestroyListeners
2209-
.forEach(listener -> listener.serviceDestroy(event));
2208+
RuntimeException exception = null;
2209+
for (ServiceDestroyListener listener : serviceDestroyListeners) {
2210+
try {
2211+
listener.serviceDestroy(event);
2212+
} catch (RuntimeException e) {
2213+
if (exception == null) {
2214+
exception = e;
2215+
} else {
2216+
e.addSuppressed(e);
2217+
}
2218+
}
2219+
}
2220+
if (exception != null) {
2221+
throw exception;
2222+
}
22102223
}
22112224

22122225
/**

‎flow-server/src/main/java/com/vaadin/flow/server/VaadinServlet.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,10 +623,13 @@ static URL getApplicationUrl(HttpServletRequest request)
623623
@Override
624624
public void destroy() {
625625
super.destroy();
626-
if (getService() != null) {
627-
getService().destroy();
626+
try {
627+
if (getService() != null) {
628+
getService().destroy();
629+
}
630+
} finally {
631+
isServletInitialized = false;
628632
}
629-
isServletInitialized = false;
630633
}
631634

632635
private VaadinServletContext initializeContext() {

‎flow-server/src/test/java/com/vaadin/flow/server/VaadinServiceTest.java‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Map;
3131
import java.util.Optional;
3232
import java.util.Set;
33+
import java.util.concurrent.atomic.AtomicInteger;
3334
import java.util.concurrent.atomic.AtomicReference;
3435
import java.util.stream.Collectors;
3536

@@ -63,6 +64,7 @@
6364

6465
import static org.hamcrest.CoreMatchers.containsString;
6566
import static org.hamcrest.MatcherAssert.assertThat;
67+
import static org.junit.Assert.assertThrows;
6668

6769
/**
6870
*
@@ -99,6 +101,34 @@ public void sessionDestroy(SessionDestroyEvent event) {
99101
}
100102
}
101103

104+
private class ThrowingSessionDestroyListener
105+
implements SessionDestroyListener {
106+
107+
@Override
108+
public void sessionDestroy(SessionDestroyEvent event) {
109+
throw new RuntimeException();
110+
}
111+
}
112+
113+
private class TestServiceDestroyListener implements ServiceDestroyListener {
114+
115+
int callCount = 0;
116+
117+
@Override
118+
public void serviceDestroy(ServiceDestroyEvent event) {
119+
callCount++;
120+
}
121+
}
122+
123+
private class ThrowingServiceDestroyListener
124+
implements ServiceDestroyListener {
125+
126+
@Override
127+
public void serviceDestroy(ServiceDestroyEvent event) {
128+
throw new RuntimeException();
129+
}
130+
}
131+
102132
private String createCriticalNotification(String caption, String message,
103133
String details, String url) {
104134
return VaadinService.createCriticalNotificationJSON(caption, message,
@@ -298,6 +328,48 @@ public void testFireSessionDestroy()
298328
1, listener.callCount);
299329
}
300330

331+
@Test
332+
public void testSessionDestroyListenerCalled_whenAnotherListenerThrows()
333+
throws ServiceException {
334+
VaadinService service = createService();
335+
336+
ThrowingSessionDestroyListener throwingListener = new ThrowingSessionDestroyListener();
337+
TestSessionDestroyListener listener = new TestSessionDestroyListener();
338+
339+
service.addSessionDestroyListener(throwingListener);
340+
service.addSessionDestroyListener(listener);
341+
342+
final AtomicInteger errorCount = new AtomicInteger();
343+
MockVaadinSession vaadinSession = new MockVaadinSession(service);
344+
vaadinSession.lock();
345+
vaadinSession.setErrorHandler(e -> errorCount.getAndIncrement());
346+
vaadinSession.unlock();
347+
service.fireSessionDestroy(vaadinSession);
348+
349+
Assert.assertEquals("ErrorHandler not called exactly once", 1,
350+
errorCount.get());
351+
352+
Assert.assertEquals("SessionDestroyListener not called exactly once", 1,
353+
listener.callCount);
354+
}
355+
356+
@Test
357+
public void testServiceDestroyListenerCalled_whenAnotherListenerThrows()
358+
throws ServletException, ServiceException {
359+
VaadinService service = createService();
360+
361+
ThrowingServiceDestroyListener throwingListener = new ThrowingServiceDestroyListener();
362+
TestServiceDestroyListener listener = new TestServiceDestroyListener();
363+
364+
service.addServiceDestroyListener(throwingListener);
365+
service.addServiceDestroyListener(listener);
366+
367+
assertThrows(RuntimeException.class, () -> service.destroy());
368+
369+
Assert.assertEquals("ServiceDestroyListener not called exactly once", 1,
370+
listener.callCount);
371+
}
372+
301373
@Test
302374
public void captionIsSetToACriticalNotification() {
303375
String notification = createCriticalNotification("foobar", "message",

0 commit comments

Comments
 (0)