Skip to content
Open
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 @@ -41,6 +41,7 @@
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -72,8 +73,11 @@ public class LocalIncomingServerSession extends LocalServerSession implements In
* List of domains, subdomains and virtual hostnames of the remote server that were
* validated with this server. The remote server is allowed to send packets to this
* server from any of the validated domains.
*
* Domains are added by the thread that processes data from the remote server, and read by other threads, such as
* the one that closes the session.
*/
private Set<String> validatedDomains = new HashSet<>();
private final Set<String> validatedDomains = ConcurrentHashMap.newKeySet();

/**
* Domains or subdomain of this server that was used by the remote server
Expand Down Expand Up @@ -337,7 +341,7 @@ public Collection<String> getValidatedDomains() {
*
* @param domain the new validated domain, subdomain or virtual host to add.
*/
public void addValidatedDomain(String domain) {
public void addValidatedDomain(@Nonnull final String domain) {
if (validatedDomains.add(domain)) {
// Set the first validated domain as the address of the session
if (validatedDomains.size() < 2) {
Expand All @@ -357,7 +361,7 @@ public void addValidatedDomain(String domain) {
* @param domain the domain, subdomain or virtual host to remove from the list of
* validated domains.
*/
public void removeValidatedDomain(String domain) {
public void removeValidatedDomain(@Nonnull final String domain) {
validatedDomains.remove(domain);
// Unregister the validated domain for this server session in SessionManager
SessionManager.getInstance().unregisterIncomingServerSession(domain, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ public abstract class LocalSession implements Session {

/**
* The current session status.
*
* It is read without a lock by threads other than the one that changes it, such as those that deliver stanzas to
* the session, and the thread of a connection that resumes the session.
*/
protected Status status = Status.CONNECTED;
protected volatile Status status = Status.CONNECTED;

/**
* The connection that this session represents.
Expand All @@ -89,7 +92,10 @@ public abstract class LocalSession implements Session {

protected final long startDate = System.currentTimeMillis();

private long lastActiveDate;
/**
* The time of the last activity of the session. It is read by the task that cleans up detached sessions.
*/
private volatile long lastActiveDate;

private AtomicLong clientPacketCount = new AtomicLong( 0 );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicLong;

/**
Expand Down Expand Up @@ -76,7 +77,12 @@ public class StreamManager {
.build();

private final Logger Log;
private boolean resume = false;

/**
* Whether the session can be resumed. It is read without a lock by threads other than the one that changes it, such
* as the thread of a connection that resumes the session, and the listener that handles the closing of a connection.
*/
private volatile boolean resume = false;

public static class UnackedPacket {
public final long x;
Expand Down Expand Up @@ -143,9 +149,11 @@ public static boolean isStreamManagementActive() {
private Deque<UnackedPacket> unacknowledgedServerStanzas = new LinkedList<>();

/**
* Delegates that can determine if a detached session can be terminated.
* Delegates that can determine if a detached session can be terminated. They are added and removed by any thread,
* and read by the task that cleans up detached sessions. A copy of this set reflects it at a single moment, so that
* a delegate that is replaced while the copy is made cannot be missed together with its replacement.
*/
private final Set<TerminationDelegate> terminationDelegates = new HashSet<>();
private final Set<TerminationDelegate> terminationDelegates = new CopyOnWriteArraySet<>();

public StreamManager(LocalSession session) {
String address;
Expand Down Expand Up @@ -769,6 +777,12 @@ public void sentStanza(Packet packet) {

synchronized (this)
{
if ( !isEnabled() ) {
// OF-3420: Stream Management was disabled after the check above, for example by onClose(), which handled the unacknowledged stanzas already.
// A stanza that is tracked after that would never be handled.
Log.trace( "Not tracking stanza of type '{}' with ID '{}', as Stream Management was disabled while it was sent. Session: {}", packet.getElement().getName(), packet.getID(), session );
return;
}
final Connection connection = session.getConnection();
if ( exceededMaximumUnacknowledgedStanzas && packet instanceof Message && connection != null && connection.isClosed() && connection.getPacketDeliverer() != null ) {
// OF-3434: The fallback of the closed connection stores this message offline. Tracking it as well would deliver it twice.
Expand Down Expand Up @@ -1001,6 +1015,9 @@ private int getMaximumUnacknowledgedStanzas()
/**
* Returns a defensive copy of all delegates that can determine if a detached session can be terminated.
*
* The copy holds the delegates that were registered at a single moment. A delegate that is added before another
* delegate is removed is therefore never missing from the copy together with that other delegate.
*
* @return all delegates that can determine if a detached session can be terminated.
*/
public Set<TerminationDelegate> getTerminationDelegates()
Expand All @@ -1019,7 +1036,7 @@ public Set<TerminationDelegate> getTerminationDelegates()
*/
public void addTerminationDelegate(@Nonnull final TerminationDelegate delegate)
{
terminationDelegates.add(delegate);
terminationDelegates.add(Objects.requireNonNull(delegate, "Argument 'delegate' cannot be null."));
}

/**
Expand All @@ -1032,7 +1049,7 @@ public void addTerminationDelegate(@Nonnull final TerminationDelegate delegate)
*/
public void removeTerminationDelegate(@Nonnull final TerminationDelegate delegate)
{
terminationDelegates.remove(delegate);
terminationDelegates.remove(Objects.requireNonNull(delegate, "Argument 'delegate' cannot be null."));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (C) 2026 Ignite Realtime Foundation. All rights reserved.
*
* 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 org.jivesoftware.openfire.session;

import org.jivesoftware.Fixtures;
import org.jivesoftware.openfire.Connection;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.spi.BasicStreamIDFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.withSettings;

/**
* Verifies that the domains that were validated for a {@link LocalIncomingServerSession} can be read while a domain is
* validated (OF-3420). Domains are validated by the thread that processes data from the remote server, while other
* threads, such as the one that closes the session, read them.
*
* @author Guus der Kinderen, guus@goodbytes.nl
*/
public class LocalIncomingServerSessionValidatedDomainsTest
{
private static final String REMOTE_DOMAIN = "remote.example.org";

private LocalIncomingServerSession session;

@BeforeAll
public static void setUpClass() throws Exception
{
Fixtures.reconfigureOpenfireHome();
Fixtures.disableDatabasePersistence();
}

@BeforeEach
public void setUp()
{
Fixtures.clearExistingProperties();

final XMPPServer xmppServer = Fixtures.mockXMPPServer();
//noinspection deprecation
XMPPServer.setInstance(xmppServer);

final Connection connection = mock(Connection.class, withSettings().lenient());
session = new LocalIncomingServerSession(Fixtures.XMPP_DOMAIN, connection, new BasicStreamIDFactory().createStreamID(), REMOTE_DOMAIN);
}

@AfterEach
public void tearDown()
{
Fixtures.clearExistingProperties();
//noinspection deprecation
XMPPServer.setInstance(null);
}

/**
* Verifies that the validated domains can be iterated while a domain is validated. The domain is validated between
* two steps of the iteration, which stands in for another thread that validates it at that moment.
*/
@Test
public void validatedDomainsCanBeIteratedWhileDomainIsValidated()
{
// Setup test fixture.
session.addValidatedDomain(REMOTE_DOMAIN);
session.addValidatedDomain("other.example.org");
final Iterator<String> iterator = session.getValidatedDomains().iterator();
final List<String> iterated = new ArrayList<>();
iterated.add(iterator.next());

// Execute system under test.
session.addValidatedDomain("third.example.org");
assertDoesNotThrow(() -> iterator.forEachRemaining(iterated::add), "Iterating the validated domains must not fail when a domain is validated at the same time.");

// Verify result.
assertTrue(iterated.containsAll(List.of(REMOTE_DOMAIN, "other.example.org")), "The domains that were validated before the iteration started must be iterated, but were: " + iterated);
assertTrue(session.isValidDomain("third.example.org"), "The domain that was validated during the iteration must be valid.");
}
}
Loading
Loading