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
10 changes: 7 additions & 3 deletions java/org/apache/catalina/ha/context/ReplicatedContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@ public ClassLoader[] getClassLoaders() {
@Override
public ServletContext getServletContext() {
if (context == null) {
context = new ReplApplContext(this);
if (getAltDDName() != null) {
context.setAttribute(Globals.ALT_DD_ATTR, getAltDDName());
synchronized (this) {
if (context == null) {
context = new ReplApplContext(this);
if (getAltDDName() != null) {
context.setAttribute(Globals.ALT_DD_ATTR, getAltDDName());
}
}
}
}

Expand Down
57 changes: 57 additions & 0 deletions test/org/apache/catalina/ha/context/TestReplicatedContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicReference;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -37,6 +42,58 @@

public class TestReplicatedContext extends TomcatBaseTest {

@Test
public void testGetServletContextReturnsSameInstanceUnderConcurrency() throws Exception {
Tomcat tomcat = getTomcatInstance();
Host host = tomcat.getHost();
if (host instanceof StandardHost) {
((StandardHost) host).setContextClass(ReplicatedContext.class.getName());
}

File root = new File("test/webapp");
ReplicatedContext replicatedContext =
(ReplicatedContext) tomcat.addWebapp(host, "", root.getAbsolutePath());
tomcat.start();

// Null the context field to simulate the window during reload
replicatedContext.context = null;

int numThreads = 20;
CyclicBarrier barrier = new CyclicBarrier(numThreads);
List<Thread> threads = new ArrayList<>();
ServletContext[] results = new ServletContext[numThreads];
AtomicReference<Throwable> failure = new AtomicReference<>();

for (int i = 0; i < numThreads; i++) {
final int index = i;
Thread thread = new Thread(() -> {
try {
barrier.await();
results[index] = replicatedContext.getServletContext();
} catch (Throwable ex) {
failure.set(ex);
}
});
thread.start();
threads.add(thread);
}

for (Thread thread : threads) {
thread.join(5000);
}

if (failure.get() != null) {
Assert.fail("Thread failed: " + failure.get());
}

ServletContext first = results[0];
Assert.assertNotNull(first);
for (int i = 1; i < numThreads; i++) {
Assert.assertSame(first, results[i]);
}
}


@Test
public void testBug57425() throws LifecycleException, IOException {
Tomcat tomcat = getTomcatInstance();
Expand Down
Loading