Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.atmosphere.cpr.AtmosphereResource.TRANSPORT;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.cpr.AtmosphereResourceImpl;
import org.atmosphere.cpr.FrameworkConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tools.jackson.core.JacksonException;
Expand Down Expand Up @@ -663,6 +664,16 @@ private static void sendNotificationAndDisconnect(
"sendNotificationAndDisconnect called for resource no longer in scope");
return;
}
if (resource.transport() == TRANSPORT.SSE

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this location need to take the transport into account if no other location needs to?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Artur- This is the only place that writes straight to the response of a push resource. All other push messages go through the Broadcaster (AtmospherePushConnection.sendMessage) to a suspended resource, and Atmosphere's interceptors add each transport's framing there, including data: for SSE.

Here the resource is usually not suspended yet (the connection is rejected in onConnect or the first receive). On that path:

  • WebSocket and long polling need no framing beyond the length prefix, and TrackMessageSizeInterceptor adds that whether or not the resource is suspended. So the plain JSON write works for them.
  • SSEAtmosphereInterceptor only writes padding on the first write to an unsuspended resource. It skips the data: prefix and the closing blank line, so EventSource drops the message. That's why only SSE needs its own branch, and only while the resource is unsuspended.

The other option would be to suspend the resource first and broadcast the notification, so Atmosphere frames it for every transport. That would change the disconnect flow for all transports to fix one, so I kept the change local. I can switch to that approach if you prefer it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would sound better not to do low level writes but how large is the blast radius?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Artur- Good point. I replaced the low-level write, so the blast radius stays the same as before: only SSE resources that are not suspended yet.

Atmosphere's SSE interceptor already frames writes on an unsuspended resource when the request has the FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL attribute. Atmosphere's JavaScriptProtocol sets it for the handshake of a new connection. So the notification now goes through the normal Atmosphere writer, and PushHandler only sets that attribute first. Atmosphere adds the padding, the data: framing and the message size prefix itself.

I checked the wire output on embedded Jetty with real Atmosphere, using a push request without a session:

  • Reconnect with an existing tracking id: before 45|{...sessionExpired...} (unframed), after data:45|{...} followed by a blank line.
  • First connection handshake (tracking id 0): data:45|{...} both before and after, because Atmosphere already set the attribute there.

So the bug only hit reconnects, which is the idle-tab case. WebSocket, long polling and already open SSE connections are untouched. A test now checks that WebSocket requests don't get the attribute.

&& !resource.isSuspended()) {
// Atmosphere's SSE interceptor frames writes as events only on
// a suspended resource, or when this attribute is set, as it
// is for the handshake of a new connection. EventSource ignores
// anything that is not framed.
resource.getRequest().setAttribute(
FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL,
Boolean.TRUE);
}
resource.getResponse()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ The notification response still declares application/json, which a browser EventSource rejects regardless of the new framing

This path never suspends the resource, and the write sets JsonConstants.JSON_CONTENT_TYPE on the response. The reconnect is a fresh HTTP request whose headers are not committed yet, so the SSE response goes out as application/json; charset=UTF-8. Per the HTML spec, EventSource fails the connection when the content type is not text/event-stream, and the message body is never delivered to the client — the framing fix would then change the wire bytes without fixing the reported symptom.

The establish path sets text/plain the same way and SSE push does work, which implies Atmosphere re-asserts text/event-stream somewhere — most plausibly around suspend, which this path never reaches. Worth confirming in a real browser (check the response headers, not just the body) rather than with a raw HTTP client.

PushHandler.java:671 · correctness

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response goes out as text/event-stream. On a resource that isn't suspended, the SSE interceptor's first write goes through writePadding, which calls response.setContentType("text/event-stream") and flushes the padding. That happens before any payload, so the JSON content type set here is replaced while the headers are still uncommitted. The curl output in the issue already shows Content-Type: text/event-stream;charset=utf-8 for this path.

I also checked it in a real browser. The new SsePushSessionExpiredIT drops the session cookie and closes the SSE connection from the server, then waits for the page to reload from the session-expired notification on the new connection. EventSource accepts the response and the page reloads. Without the fix, the page never reloads and the test fails.

.setContentType(JsonConstants.JSON_CONTENT_TYPE);
resource.getResponse().getWriter().write(notificationJson);
Expand Down
8 changes: 6 additions & 2 deletions flow-tests/MIGRATION_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ procedure in [README.md](README.md) and the permutation policy in

| Target | ITs |
|---|---|
| test-default | 368 |
| test-default | 372 |
| ?UNMAPPED? | 33 |
| test-embedding | 27 |
| test-production | 23 |
Expand Down Expand Up @@ -46,7 +46,7 @@ procedure in [README.md](README.md) and the permutation policy in
| test-plain-spring | 1 |
| test-push-startup(keep) | 1 |
| test-tailwind | 1 |
| **TOTAL** | **631** |
| **TOTAL** | **635** |

## Mapping (grouped by target module, then feature)

Expand Down Expand Up @@ -496,6 +496,10 @@ procedure in [README.md](README.md) and the permutation policy in
| PushLargeDataWebsocketIT | test-root-context | move |
| PushLongPollingUpdateDivIT | test-root-context | move |
| PushLongPollingWithPreserveOnRefreshIT | test-root-context | move |
| PushSessionExpiredIT | test-root-context | move |
| PushSessionExpiredLongPollingIT | test-root-context | move |
| PushSessionExpiredSseIT | test-root-context | move |
| PushSessionExpiredWebsocketIT | test-root-context | move |
| PushSettingsIT | test-root-context | move |
| PushToggleComponentVisibilityIT | test-root-context | move |
| PushUnloadPolicyIT | test-root-context | move |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.uitest.servlet.ViewTestLayout;
import com.vaadin.flow.uitest.ui.push.components.ClientServerCounter;
Expand All @@ -25,6 +26,14 @@
@Route(value = "com.vaadin.flow.uitest.ui.push.BasicPushView", layout = ViewTestLayout.class)
public class BasicPushView extends ClientServerCounter {

public static final String CLOSE_PUSH_LATER_ID = "close-push-later";

/**
* Delay before the push connection is closed, giving the test time to
* prepare the reconnect.
*/
public static final int CLOSE_PUSH_DELAY_MS = 2000;

@Override
protected void onAttach(AttachEvent attachEvent) {
UI ui = attachEvent.getUI();
Expand All @@ -36,5 +45,22 @@ protected void onAttach(AttachEvent attachEvent) {

ui.getPushConfiguration().setPushMode(push.value());
ui.getPushConfiguration().setTransport(push.transport());

// Lets a test close the push connection from the server, so that the
// client opens a new one
NativeButton closePushLater = new NativeButton(
"Close push connection in " + CLOSE_PUSH_DELAY_MS + " ms",
event -> new Thread(() -> {
try {
Thread.sleep(CLOSE_PUSH_DELAY_MS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
ui.access(() -> ui.getInternals().getPushConnection()
.disconnect());
}).start());
closePushLater.setId(CLOSE_PUSH_LATER_ID);
add(closePushLater);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2000-2026 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.uitest.ui.push;

import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;

import com.vaadin.flow.testcategory.PushTests;
import com.vaadin.flow.testutil.ChromeBrowserTest;

@Category(PushTests.class)
public abstract class PushSessionExpiredIT extends ChromeBrowserTest {

@Test
public void sessionGone_pushReconnects_pageReloads() {
open();
AbstractClientServerCounterIT.getIncrementButton(this).click();
waitUntil(driver -> AbstractClientServerCounterIT
.getClientCounter(this) == 1);
// Survives push reconnects, but not a reload
executeScript("window.beforeSessionGone = true");

// As after a server restart: the server closes the push connection,
// and the session is gone when the client opens a new one
findElement(By.id(BasicPushView.CLOSE_PUSH_LATER_ID)).click();
getDriver().manage().deleteCookieNamed("JSESSIONID");

// The new push connection is answered with the session expired
// notification, which reloads the page by default. If the client
// misses the notification, it keeps opening new push connections and
// the page is not reloaded.
try {
waitUntil(
driver -> Boolean.TRUE.equals(executeScript(
"return window.beforeSessionGone === undefined")),
20);
} catch (TimeoutException e) {
Assert.fail("The page was not reloaded after the session was gone");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2026 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.uitest.ui.push;

import org.junit.experimental.categories.Category;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.testcategory.IgnoreOSGi;
import com.vaadin.flow.testcategory.PushTests;

@Category({ PushTests.class, IgnoreOSGi.class })
public class PushSessionExpiredLongPollingIT extends PushSessionExpiredIT {

@Override
protected Class<? extends Component> getViewClass() {
return BasicPushLongPollingView.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2026 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.uitest.ui.push;

import org.junit.experimental.categories.Category;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.testcategory.IgnoreOSGi;
import com.vaadin.flow.testcategory.PushTests;

@Category({ PushTests.class, IgnoreOSGi.class })
public class PushSessionExpiredSseIT extends PushSessionExpiredIT {

@Override
protected Class<? extends Component> getViewClass() {
return BasicPushSseView.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2026 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.uitest.ui.push;

import org.junit.experimental.categories.Category;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.testcategory.IgnoreOSGi;
import com.vaadin.flow.testcategory.PushTests;

@Category({ PushTests.class, IgnoreOSGi.class })
public class PushSessionExpiredWebsocketIT extends PushSessionExpiredIT {

@Override
protected Class<? extends Component> getViewClass() {
return BasicPushWebsocketView.class;
}
}
Loading