The RabbitMQ server logs report a session closure due to an "amqp:session:unattached-handle" error. This is typically accompanied by a warning about receiving a Flow frame for an unknown link handle.
[warning] Received Flow frame for unknown link handle: {'v1_0.flow', ... {uint,7}, ...}
[warning] Closing session for connection: {symbol, <<"amqp:session:unattached-handle">>}
[warning] {utf8, <<"Unattached link handle: 7">>}
This error occurs when the AMQP 1.0 client sends a command (usually a Flow frame to request/provide credit) for a Link Handle that the RabbitMQ server does not recognize as active.
In highly asynchronous environments (like ProtonJ2), this is most commonly caused by an Execution Race Condition during shutdown:
The client application initiates a stop() or close() command.
The test runner or application moves to the next task before the shutdown handshake is complete.
The client’s background I/O thread sends a final Flow frame for a link that the server has already started tearing down or has discarded.
RabbitMQ receives a command for a "dead" handle and must terminate the session per the AMQP 1.0 specification.
Ensure that the client fully completes the shutdown handshake before allowing the thread or test to proceed. Use a CountDownLatch or a Future to block until the container/connection is fully stopped.
Fix Example (Java/ProtonJ2):
// DO NOT do this:
// listenerContainer.stop();
// DO THIS:
CountDownLatch stopLatch = new CountDownLatch(1);
listenerContainer.stop(stopLatch::countDown);
if (!stopLatch.await(30, TimeUnit.SECONDS)) {
throw new RuntimeException("Timed out waiting for container to stop");
}