Skip to content

Commit 7e89dfe

Browse files
author
Denis
authored
fix: reset HTTP session on CLOSED state, not immidiately (#11377)
fixes #6959
1 parent 813dd86 commit 7e89dfe

3 files changed

Lines changed: 74 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ && getCurrent() == this) {
202202
// it as soon as we acquire the lock.
203203
service.fireSessionDestroy(this);
204204
}
205-
session = null;
206205
}
207206

208207
/**
@@ -887,6 +886,10 @@ assert isValidChange(state) : "Invalid session state change "
887886
+ this.state + "->" + state;
888887

889888
this.state = state;
889+
890+
if (VaadinSessionState.CLOSED.equals(state)) {
891+
session = null;
892+
}
890893
}
891894

892895
private boolean isValidChange(VaadinSessionState newState) {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Optional;
2727
import java.util.Set;
2828
import java.util.concurrent.atomic.AtomicBoolean;
29+
import java.util.concurrent.atomic.AtomicReference;
2930
import java.util.stream.Collectors;
3031
import java.util.stream.Stream;
3132

@@ -162,6 +163,24 @@ public void testFireSessionDestroy()
162163
1, listener.callCount);
163164
}
164165

166+
@Test
167+
public void fireSessionDestroy_sessionStateIsSetToClosed()
168+
throws ServletException, ServiceException {
169+
VaadinService service = createService();
170+
171+
AtomicReference<VaadinSessionState> stateRef = new AtomicReference<>();
172+
MockVaadinSession vaadinSession = new MockVaadinSession(service) {
173+
@Override
174+
protected void setState(VaadinSessionState state) {
175+
stateRef.set(state);
176+
}
177+
};
178+
179+
service.fireSessionDestroy(vaadinSession);
180+
181+
Assert.assertEquals(VaadinSessionState.CLOSED, stateRef.get());
182+
}
183+
165184
@Test
166185
public void captionIsSetToACriticalNotification() {
167186
String notification = createCriticalNotification("foobar", "message",

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,55 @@ public void csrfToken_same_session_shouldBeSame() {
376376
"getCsrfToken() should always return the same value for the same session",
377377
token1, token2);
378378
}
379+
380+
@Test
381+
public void valueUnbound_wrappedSessionIsNotCleanedUp() {
382+
ReentrantLock lock = Mockito.mock(ReentrantLock.class);
383+
Mockito.when(lock.isHeldByCurrentThread()).thenReturn(true);
384+
mockService = new MockVaadinServletService() {
385+
@Override
386+
protected Lock getSessionLock(WrappedSession wrappedSession) {
387+
return lock;
388+
}
389+
};
390+
391+
VaadinSession vaadinSession = new VaadinSession(mockService);
392+
393+
WrappedSession httpSession = Mockito.mock(WrappedSession.class);
394+
vaadinSession.refreshTransients(httpSession, mockService);
395+
396+
VaadinSession.setCurrent(vaadinSession);
397+
mockService.setCurrentInstances(Mockito.mock(VaadinRequest.class),
398+
Mockito.mock(VaadinResponse.class));
399+
400+
try {
401+
vaadinSession
402+
.valueUnbound(Mockito.mock(HttpSessionBindingEvent.class));
403+
404+
Assert.assertNotNull(vaadinSession.getSession());
405+
} finally {
406+
CurrentInstance.clearAll();
407+
}
408+
}
409+
410+
@Test
411+
public void setState_closedState_sessionAttributeIsCleanedUp() {
412+
ReentrantLock lock = Mockito.mock(ReentrantLock.class);
413+
Mockito.when(lock.isHeldByCurrentThread()).thenReturn(true);
414+
mockService = new MockVaadinServletService() {
415+
@Override
416+
protected Lock getSessionLock(WrappedSession wrappedSession) {
417+
return lock;
418+
}
419+
};
420+
421+
VaadinSession vaadinSession = new VaadinSession(mockService);
422+
WrappedSession httpSession = Mockito.mock(WrappedSession.class);
423+
vaadinSession.refreshTransients(httpSession, mockService);
424+
425+
vaadinSession.setState(VaadinSessionState.CLOSING);
426+
vaadinSession.setState(VaadinSessionState.CLOSED);
427+
428+
Assert.assertNull(vaadinSession.getSession());
429+
}
379430
}

0 commit comments

Comments
 (0)