According to https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.publish, when publishing,
If the exchange name is specified, and that exchange does not exist, the server will raise a channel exception.
Using pika this only occurs the second time the message is sent to a missing exchange over the same channel:
(env36x64) C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.4\sbin>rabbitmqctl.bat list_exchanges name
Listing exchanges for vhost / ...
amq.fanout
amq.match
amq.headers
amq.rabbitmq.trace
amq.topic
amq.direct
(env36x64) C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.4\sbin>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import pika
>>> creds = pika.PlainCredentials('user', 'passw')
>>> params = pika.ConnectionParameters(host='ms7', port=5672, credentials=creds)
>>> conn = pika.BlockingConnection(params)
>>> ch = conn.channel()
>>> ch.basic_publish(exchange='invalid', routing_key='', body='hello')
>>> # No error. Not expected
>>> # Second attempt does raise exception, as expected
>>> ch.basic_publish(exchange='invalid', routing_key='', body='hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Administrator\Documents\env36x64\lib\site-packages\pika\adapters\blocking_connection.py", line 2239, in basic_publish
self._flush_output()
File "C:\Users\Administrator\Documents\env36x64\lib\site-packages\pika\adapters\blocking_connection.py", line 1331, in _flush_output
raise self._closing_reason # pylint: disable=E0702
pika.exceptions.ChannelClosedByBroker: (404, "NOT_FOUND - no exchange 'invalid' in vhost '/'")
If you do a packet capture with Wireshark, it explains the behavior you're seeing. The Channel.Close
sent from RabbitMQ to your test app is asynchronous, so your second basic_publish
call executes before the first Channel.Close
is received. There is no bug here in either RabbitMQ or Pika.