pika/pika · error · AMQPConnectorWrongState

Cannot abort before starting.

Error message

Cannot abort before starting.

What it means

Raised by AMQPConnector.abort() (connection_workflow.py:224) when its internal state is _STATE_INIT, meaning start() has not yet been invoked on this connector. pika's connector is a state machine and aborting before start is a programming error because there is no I/O loop task or transport to tear down. It is surfaced as AMQPConnectorWrongState, a pika.exceptions-derived class.

Source

Thrown at pika/adapters/utils/connection_workflow.py:237

        self._stack_timeout_ref = None
        if self._conn_params.stack_timeout is not None:
            self._stack_timeout_ref = self._nbio.call_later(
                self._conn_params.stack_timeout, self._on_overall_timeout)

    def abort(self) -> None:
        """
        Abort the workflow asynchronously.

        The completion callback will be called with an instance of AMQPConnectorAborted.

        NOTE: we can't cancel/close synchronously because aborting pika
        Connection and its transport requires an asynchronous operation.

        :raises AMQPConnectorWrongState: If called after completion has been
            reported or the workflow not started yet.
        """
        if self._state == self._STATE_INIT:
            raise AMQPConnectorWrongState('Cannot abort before starting.')

        if self._state == self._STATE_DONE:
            raise AMQPConnectorWrongState(
                'Cannot abort after completion was reported')

        self._state = self._STATE_ABORTING
        self._deactivate()

        assert self._conn_params is not None
        assert self._nbio is not None
        _LOG.info(
            'AMQPConnector: beginning client-initiated asynchronous '
            'abort; %r/%s', self._conn_params.host, self._addr_record)

        if self._amqp_conn is None:
            _LOG.debug('AMQPConnector.abort(): no connection, so just '
                       'scheduling completion report via I/O loop.')
            self._nbio.add_callback_threadsafe(

View on GitHub (pinned to 295ad9e579)

Solutions

  1. Only call abort() after start() has been called; track a 'started' flag.
  2. In cleanup/finally blocks, guard with: if connector is not None and started: connector.abort().
  3. Prefer the public adapter API (Connection.connect / ioloop-based) which manages the connector lifecycle for you.

Example fix

# before
connector = AMQPConnector(...)
try:
    do_setup()
    connector.start(...)
finally:
    connector.abort()  # raises if do_setup() threw

# after
connector = AMQPConnector(...)
started = False
try:
    do_setup()
    connector.start(...)
    started = True
finally:
    if started:
        connector.abort()
Defensive patterns

Strategy: validation

Validate before calling

started = False
connector = AMQPConnector(...)
try:
    connector.start(...)
    started = True
finally:
    if started:
        connector.abort()

Type guard

def can_abort(connector) -> bool:
    # AMQPConnector exposes its state via _state; INIT means not started.
    return getattr(connector, '_state', None) != connector._STATE_INIT and \
           getattr(connector, '_state', None) != connector._STATE_DONE

Try / catch

from pika.adapters.utils.connection_workflow import AMQPConnectorWrongState

try:
    connector.abort()
except AMQPConnectorWrongState as e:
    if 'Cannot abort before starting' in str(e):
        logger.debug('connector never started; nothing to abort')
    else:
        raise

Prevention

When it happens

Trigger: Calling connector.abort() on a freshly constructed AMQPConnector before calling its start() method. Typically happens in custom workflows or tests that instantiate AMQPConnector directly and call abort() in a finally block before start ran (e.g. because an earlier setup step raised).

Common situations: Test teardown calling abort() unconditionally in tearDown; exception handlers that abort a connector whose start() was skipped due to an upstream error; misuse of the internal AMQPConnector API instead of the public adapter API.

Related errors


AI-assisted analysis of pika/pika@295ad9e579 (2026-08-04). Data as JSON: /data/errors/b5fa546e2c661188.json. Report an issue: GitHub.