Skip to content

Commit 90e05cc

Browse files
authored
fix: do not close external filesystems (#16970)
If someone else has opened a fileSystem, do not close it after we are done. Thanks to @oliveryasuna for the test module. Fixes #16908
1 parent 8b02573 commit 90e05cc

5 files changed

Lines changed: 263 additions & 4 deletions

File tree

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
import java.nio.file.Paths;
3434
import java.util.Collections;
3535
import java.util.HashMap;
36+
import java.util.HashSet;
3637
import java.util.Map;
38+
import java.util.Set;
3739
import java.util.regex.Matcher;
3840
import java.util.regex.Pattern;
3941

@@ -88,6 +90,7 @@ public class StaticFileServer implements StaticFileHandler {
8890

8991
// Mapped uri is for the jar file
9092
static final Map<URI, Integer> openFileSystems = new HashMap<>();
93+
static final Set<URI> externalFileSystem = new HashSet<>();
9194
private static final Object fileSystemLock = new Object();
9295

9396
/**
@@ -206,7 +209,12 @@ private FileSystem getNewOrExistingFileSystem(URI resourceURI)
206209
getLogger().trace(
207210
"Tried to get new filesystem, but it already existed for target uri.",
208211
fsaee);
209-
return FileSystems.getFileSystem(resourceURI);
212+
FileSystem fileSystem = FileSystems.getFileSystem(resourceURI);
213+
URI fileUri = getFileURI(resourceURI);
214+
if (!openFileSystems.containsKey(fileUri)) {
215+
externalFileSystem.add(fileUri);
216+
}
217+
return fileSystem;
210218
}
211219
}
212220

@@ -219,9 +227,12 @@ void closeFileSystem(URI resourceURI) {
219227
(key, value) -> value - 1);
220228
if (locks != null && locks == 0) {
221229
openFileSystems.remove(fileURI);
222-
// Get filesystem is for the file to get the correct
223-
// provider
224-
FileSystems.getFileSystem(resourceURI).close();
230+
if (!externalFileSystem.contains(fileURI)) {
231+
// Get filesystem is for the file to get the correct
232+
// provider
233+
FileSystems.getFileSystem(resourceURI).close();
234+
}
235+
externalFileSystem.remove(fileURI);
225236
}
226237
} catch (IOException ioe) {
227238
getLogger().error("Failed to close FileSystem for '{}'",

‎flow-tests/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@
294294

295295
<module>test-scalability</module>
296296
<module>test-servlet</module>
297+
<module>test-embedded-jetty-12</module>
297298
<module>test-themes</module>
298299
<module>test-themes/pom-production.xml</module>
299300
<module>test-themes/pom-devbundle.xml</module>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2000-2023 Vaadin Ltd.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License"); you may not
6+
~ use this file except in compliance with the License. You may obtain a copy of
7+
~ the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
~ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
~ License for the specific language governing permissions and limitations under
15+
~ the License.
16+
-->
17+
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
22+
<modelVersion>4.0.0</modelVersion>
23+
<parent>
24+
<groupId>com.vaadin</groupId>
25+
<artifactId>flow-tests</artifactId>
26+
<version>24.2-SNAPSHOT</version>
27+
</parent>
28+
29+
<artifactId>test-embedded-jetty-12</artifactId>
30+
<packaging>jar</packaging>
31+
32+
<properties>
33+
<maven.deploy.skip>true</maven.deploy.skip>
34+
35+
<jetty.version>12.0.0.beta1</jetty.version>
36+
</properties>
37+
38+
<dependencyManagement>
39+
<dependencies>
40+
<dependency>
41+
<groupId>org.eclipse.jetty.ee10</groupId>
42+
<artifactId>jetty-ee10-bom</artifactId>
43+
<version>${jetty.version}</version>
44+
45+
<type>pom</type>
46+
<scope>import</scope>
47+
</dependency>
48+
</dependencies>
49+
</dependencyManagement>
50+
51+
<dependencies>
52+
<dependency>
53+
<groupId>com.vaadin</groupId>
54+
<artifactId>flow-test-resources</artifactId>
55+
<version>${project.version}</version>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>com.vaadin</groupId>
60+
<artifactId>vaadin-dev-server</artifactId>
61+
<version>${project.version}</version>
62+
</dependency>
63+
64+
<dependency>
65+
<groupId>com.vaadin</groupId>
66+
<artifactId>test-dev-bundle</artifactId>
67+
<version>${project.version}</version>
68+
</dependency>
69+
70+
<dependency>
71+
<groupId>org.eclipse.jetty</groupId>
72+
<artifactId>jetty-client</artifactId>
73+
<version>${jetty.version}</version>
74+
</dependency>
75+
76+
<dependency>
77+
<groupId>org.eclipse.jetty.ee10</groupId>
78+
<artifactId>jetty-ee10-webapp</artifactId>
79+
</dependency>
80+
81+
<dependency>
82+
<groupId>org.eclipse.jetty.ee10.websocket</groupId>
83+
<artifactId>jetty-ee10-websocket-jakarta-server</artifactId>
84+
</dependency>
85+
</dependencies>
86+
87+
<build>
88+
<resources>
89+
<resource>
90+
<directory>src/test/webapp</directory>
91+
<targetPath>webapp</targetPath>
92+
</resource>
93+
</resources>
94+
95+
<plugins>
96+
<plugin>
97+
<groupId>com.vaadin</groupId>
98+
<artifactId>flow-maven-plugin</artifactId>
99+
</plugin>
100+
</plugins>
101+
</build>
102+
103+
</project>
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright 2000-2023 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.vaadin.flow.server;
18+
19+
import org.eclipse.jetty.client.ContentResponse;
20+
import org.eclipse.jetty.client.HttpClient;
21+
import org.eclipse.jetty.client.transport.HttpClientTransportOverHTTP;
22+
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
23+
import org.eclipse.jetty.ee10.servlet.ServletHolder;
24+
import org.eclipse.jetty.ee10.webapp.WebAppContext;
25+
import org.eclipse.jetty.io.ClientConnector;
26+
import org.eclipse.jetty.server.Server;
27+
import org.eclipse.jetty.server.ServerConnector;
28+
import org.eclipse.jetty.util.SocketAddressResolver;
29+
import org.eclipse.jetty.util.resource.ResourceFactory;
30+
import org.eclipse.jetty.util.thread.QueuedThreadPool;
31+
import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler;
32+
import org.junit.After;
33+
import org.junit.Assert;
34+
import org.junit.Test;
35+
36+
public final class StaticFileServerTests {
37+
38+
private Server server;
39+
40+
private ServerConnector connector;
41+
42+
private HttpClient client;
43+
44+
private void start() throws Exception {
45+
startServer();
46+
startClient();
47+
}
48+
49+
private void startServer() throws Exception {
50+
server = new Server(new QueuedThreadPool());
51+
connector = new ServerConnector(server);
52+
53+
connector.setPort(0);
54+
55+
server.addConnector(connector);
56+
server.setHandler(createContext());
57+
58+
server.start();
59+
}
60+
61+
private ServletContextHandler createContext() {
62+
final WebAppContext context = new WebAppContext();
63+
64+
context.setBaseResource(ResourceFactory.root().newResource(
65+
StaticFileServerTests.class.getResource("/webapp")));
66+
context.setContextPath("/");
67+
context.setExtractWAR(false);
68+
69+
final ServletHolder vaadinServletHolder = context
70+
.addServlet(VaadinServlet.class, "/*");
71+
72+
vaadinServletHolder.setInitOrder(1);
73+
vaadinServletHolder.setInitParameter("pushMode", "automatic");
74+
75+
context.setAttribute(
76+
"org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
77+
".*\\.jar|.*/classes/.*");
78+
context.setConfigurationDiscovered(true);
79+
context.setParentLoaderPriority(true);
80+
81+
return context;
82+
}
83+
84+
private void startClient() throws Exception {
85+
final ClientConnector connector = new ClientConnector();
86+
87+
connector.setSelectors(1);
88+
connector.setExecutor(new QueuedThreadPool());
89+
connector.setScheduler(
90+
new ScheduledExecutorScheduler("client-scheduler", false));
91+
92+
client = new HttpClient(new HttpClientTransportOverHTTP(connector));
93+
94+
client.setSocketAddressResolver(new SocketAddressResolver.Sync());
95+
96+
client.start();
97+
}
98+
99+
@Test
100+
public void serveJarResourceTwice() throws Exception {
101+
start();
102+
103+
client.setConnectBlocking(true);
104+
105+
String content = null;
106+
107+
for (int i = 1; i <= 4; i++) {
108+
final ContentResponse response = client
109+
.GET("http://localhost:" + connector.getLocalPort()
110+
+ "/VAADIN/static/push/vaadinPush.js");
111+
112+
Assert.assertNotNull("Should not receive null response.", response);
113+
Assert.assertEquals("Expected ok status for request " + i, 200,
114+
response.getStatus());
115+
116+
if (content == null) {
117+
content = response.getContentAsString();
118+
} else {
119+
Assert.assertEquals(
120+
"Content for request " + i + " differs from original",
121+
content, response.getContentAsString());
122+
}
123+
}
124+
}
125+
126+
@After
127+
public void disposeClient() throws Exception {
128+
if (client != null) {
129+
client.stop();
130+
131+
client = null;
132+
}
133+
}
134+
135+
@After
136+
public void disposeServer() throws Exception {
137+
if (server != null) {
138+
server.stop();
139+
140+
server = null;
141+
}
142+
}
143+
144+
}

‎flow-tests/test-embedded-jetty-12/src/test/webapp/empty.txt‎

Whitespace-only changes.

0 commit comments

Comments
 (0)