Repository navigation
Description
Problem
After Manager restarts while MQTT clients are publishing, some clients cannot reconnect with their original client IDs. The same IDs can still be refused in a fresh load test after the first test has stopped. A manual connection using one of the affected IDs also reproduced the refusal.
EKS reproduction and validation
Both runs used the same load2 dataset, JMeter scenario, and 250 client IDs. All 250 clients were publishing with zero connect failures before Manager was killed about two minutes into the test. A fresh run then retried the same client IDs.
| Measure | main at 68ec88625 |
Same main plus the guard (4dcc05b70) |
|---|---|---|
| Original IDs with a successful connect after the kill | 205/250 | 250/250 |
| IDs that ended the restart run with a failed connect and never connected in the fresh run | 45 | 0 |
| IDs that connected in the fresh run | 205/250 | 250/250 |
On main, the fresh run logged 605 occurrences of Server closed connection without DISCONNECT. With the guard, all 250 original IDs reconnected and published after the kill, and the fresh run completed with zero connect or publish failures. The main reproduction establishes that the startup-sequence changes in #3587 are unnecessary to trigger this failure.
Artemis reproduction and OpenRemote workaround
The standalone probe reproduced the bug on Apache Artemis 2.55.0 and 2.57.0. It starts an embedded broker on loopback with allowLinkStealing=false, pauses a synthetic MQTT 3.1.1 authentication callback, closes the client socket, waits for transport cleanup, and releases authentication. Artemis then reports the original client ID as claimed with zero live connections for that ID. The same-ID retry receives no CONNACK. A fresh ID with the same credentials receives 20020000, a successful CONNACK.
Expected behavior is for the original ID to be available after transport cleanup and for a fresh connection with that ID to succeed. We plan to file the underlying race upstream with Apache Artemis.
The OpenRemote workaround checks whether the transport is still open before accepting the authentication result and attaching the authenticated subject. This covers the reproduced window where the socket already closed during authentication.
Standalone Java reproducer
Save the source as ReconnectProbe.java. With JDK 21, run it against the lib directory of the Artemis distribution you want to test.
java --class-path "$ARTEMIS_HOME/lib/*" ReconnectProbe.javaimport java.io.*;
import java.net.*;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import javax.security.auth.Subject;
import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl;
import org.apache.activemq.artemis.core.protocol.mqtt.MQTTStateManager;
import org.apache.activemq.artemis.core.remoting.FailureListener;
import org.apache.activemq.artemis.core.remoting.impl.netty.NettyAcceptor;
import org.apache.activemq.artemis.core.security.*;
import org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ;
import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;
import org.apache.activemq.artemis.spi.core.security.ActiveMQSecurityManager5;
import org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal;
class ReconnectProbe {
static class Auth implements ActiveMQSecurityManager5 {
final CountDownLatch entered = new CountDownLatch(1);
final CountDownLatch closed = new CountDownLatch(1);
final CountDownLatch release = new CountDownLatch(1);
final AtomicReference<RemotingConnection> first = new AtomicReference<>();
public boolean validateUser(String user, String password) { return true; }
public boolean validateUserAndRole(String user, String password, Set<Role> roles, CheckType type) { return true; }
public boolean authorize(Subject subject, Set<Role> roles, CheckType type, String address) { return true; }
public Subject authenticate(String user, String password, RemotingConnection connection, String domain) {
if ("probe".equals(user) && connection != null && first.compareAndSet(null, connection)) {
connection.addFailureListener(new FailureListener() {
public void connectionFailed(ActiveMQException e, boolean failover) { closed.countDown(); }
public void connectionFailed(ActiveMQException e, boolean failover, String node) { closed.countDown(); }
});
entered.countDown();
try {
if (!release.await(10, TimeUnit.SECONDS)) throw new IllegalStateException("probe did not release auth");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
}
}
return new Subject(true, Set.of(new UserPrincipal(user)), Set.of(), Set.of());
}
}
static Socket connect(int port, String clientId) throws IOException {
var body = new ByteArrayOutputStream();
try (var out = new DataOutputStream(body)) {
out.writeUTF("MQTT");
out.writeByte(4);
out.writeByte(0xc2);
out.writeShort(30);
out.writeUTF(clientId);
out.writeUTF("probe");
out.writeUTF("test");
}
var socket = new Socket("127.0.0.1", port);
socket.setSoTimeout(3000);
var out = socket.getOutputStream();
out.write(0x10);
out.write(body.size());
body.writeTo(out);
out.flush();
return socket;
}
static String reply(int port, String id) throws IOException {
try (var socket = connect(port, id)) {
var bytes = socket.getInputStream().readNBytes(4);
if (bytes.length == 4 && bytes[3] == 0) {
socket.getOutputStream().write(new byte[]{(byte) 0xe0, 0});
}
return bytes.length == 0 ? "closed without CONNACK" : HexFormat.of().formatHex(bytes);
}
}
static void run() throws Exception {
var auth = new Auth();
var dir = Files.createTempDirectory("arto-mqtt-stale-id-");
var cfg = new ConfigurationImpl().setPersistenceEnabled(false).setSecurityEnabled(true)
.setAuthenticationCacheSize(0).setJournalDirectory(dir.resolve("journal").toString())
.setBindingsDirectory(dir.resolve("bindings").toString()).setPagingDirectory(dir.resolve("paging").toString())
.setLargeMessagesDirectory(dir.resolve("large").toString())
.addAcceptorConfiguration("probe", "tcp://127.0.0.1:0?protocols=MQTT&allowLinkStealing=false&useEpoll=false&useKQueue=false");
var embedded = new EmbeddedActiveMQ().setConfiguration(cfg).setSecurityManager(auth);
try {
embedded.start();
var server = embedded.getActiveMQServer();
var port = ((NettyAcceptor) server.getRemotingService().getAcceptor("probe")).getActualPort();
var state = MQTTStateManager.getInstance(server);
var id = "authAbortProbe";
try (var first = connect(port, id)) {
if (!auth.entered.await(4, TimeUnit.SECONDS)) throw new IllegalStateException("CONNECT never reached authentication");
first.setSoLinger(true, 0);
}
if (!auth.closed.await(4, TimeUnit.SECONDS)) throw new IllegalStateException("Server never detected closed transport");
System.out.println("After transport cleanup, before auth resumes: claimed=" + state.isClientConnected(id));
auth.release.countDown();
long until = System.nanoTime() + TimeUnit.SECONDS.toNanos(4);
while (!state.isClientConnected(id) && System.nanoTime() < until) Thread.sleep(10);
Thread.sleep(100);
long live = server.getRemotingService().getConnections().stream()
.filter(connection -> id.equals(connection.getClientID())).count();
System.out.println("After auth resumes: claimed=" + state.isClientConnected(id) + ", listed connections=" + live);
System.out.println("Retry same ID: " + reply(port, id));
System.out.println("Fresh ID: " + reply(port, "freshProbe"));
} finally {
auth.release.countDown();
embedded.stop();
try (var paths = Files.walk(dir)) {
for (var path : paths.sorted(Comparator.reverseOrder()).toList()) Files.delete(path);
}
}
}
public static void main(String[] args) throws Exception { run(); }
}
Activity
ebariaux commentedon Nov 23, 2026
There is a second race between X.509 provisioning and disconnect cleanup, tracked independently in #3607. The device has already connected anonymously when Manager starts looking up the service-user token. If the socket closes and disconnect cleanup finishes during that lookup, the transport guard proposed here returns
null. Provisioning currently ignores that rejection, so it can record the closed connection as authenticated after cleanup and attempt to publish a success response.This timing can leave the old connection in OpenRemote’s provisioning map when the device reconnects. The duplicate-client-ID rejection reproduced above comes from Artemis’s separate connected-client registry during the initial MQTT
CONNECT.