Skip to content

ActiveMQ: a service that starts before its broker never receives, even once the broker is up #215

Backlog

Description

@christopherread

What happens

A service on Obvs.ActiveMQ 7.0.2 that starts while its broker isn't accepting connections yet never receives any messages, even after the broker comes up. It logs one error for each source, then nothing:

fail: Obvs.Bus[0]
      The Obvs bus reported an error: Error receiving Obvs.Types.ICommand from endpoint ServiceEndpoint[IServiceMessage1]
       ---> Apache.NMS.NMSConnectionException: Error connecting to amq:61616.
         at Apache.NMS.ActiveMQ.ConnectionFactory.CreateConnection(String userName, String password)
         at Obvs.ActiveMQ.Configuration.ConnectionFactoryExtensions.<>c__DisplayClass0_0.<CreateLazyConnection>b__0()
         at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
         ...
         at Obvs.ActiveMQ.MessageSource`1.<.ctor>b__7_1(IObserver`1 observer)
fail: Obvs.Bus[0]
      The Obvs bus reported an error: Error receiving Obvs.Types.IRequest from endpoint ServiceEndpoint[IServiceMessage1]
      ...
info: Microsoft.Hosting.Lifetime[0]
      Application started.

The host keeps running and looks healthy, but commands sent to it are never handled, and requests time out (No Response1 response to Request1 within 00:00:10). It doesn't recover until it's restarted.

How to reproduce

The examples showed this before #214 added a healthcheck: docker compose up started the service as soon as the ActiveMQ container started, before the broker was listening on 61616. Without Docker:

  1. Start Examples/Service.ActiveMQ with no broker running.
  2. Start a broker: docker run -d -p 61616:61616 apache/activemq-classic:6.2.0.
  3. Run Examples/Client and type a line. It's never received.

Likely cause

Not confirmed yet:

  • ConnectionFactoryExtensions.CreateLazyConnection (Obvs.ActiveMQ/Configuration/ConnectionFactoryExtensions.cs) wraps CreateConnection in a Lazy<IConnection> with LazyThreadSafetyMode.ExecutionAndPublication. That mode caches an exception from the factory, so after a failed first connect, every later .Value rethrows that exception without trying to connect again.
  • The bus does resubscribe to a source that errors (CatchAndHandle in Obvs/Extensions/ObservableExtensions.cs, through EndpointExtensions). But MessageSource reads _lazyConnection.Value on each subscribe, so a resubscription can't get past the cached exception.
  • Only one error was logged for each source, not one for each retry. So it's worth checking what the resubscription does there too.
  • The publishers share the same lazy connection, so a client started before its broker probably can't send either. I haven't checked this.

What would fix it

  • Don't cache a failed connect. For example, use LazyThreadSafetyMode.PublicationOnly, or a connection holder that tries again after a failure.
  • Retry with a delay, so a broker that's down doesn't turn into a tight loop of resubscriptions. The JetStream source does this (PR Obvs.NATS JetStream: keep receiving through lost connections #141), and the same note there about Publish().RefCount() holding a terminated subject applies to ActiveMQ's topic sources, which use PublishRefCountRetriable.
  • Add a broker test: start the bus with the broker down, start the broker, and assert a command is received.

Whether failover: broker URIs avoid this hasn't been checked either.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions