Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic_publish of BlockingConnection does not raise exception after server down #357

Closed
qwang2505 opened this issue Jun 6, 2013 · 9 comments

Comments

@qwang2505
Copy link

We're using pika to connect rabbitmq, but in some condition, there's a chance that the basic_publish method still works even when server's down, and after restarting, the messages are missing. I tested with both the latest stable version(0.9.13) and the code in master in github.

Here is the script I used for test:

#!/usr/bin/env python
import pika
import time

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

count = 0
while True:
    try:
        print channel.basic_publish(exchange='', routing_key='hello', body='Hello World!')
        count += 1
        print 'send %d messages' % count
    except:
        break
    time.sleep(5)
connection.close()
print 'total send %d messages' % count

After the connection was made, I stopped the rabbitmq-server, but it kept printing "send N messages". I tested under ubuntu 12.04, python version is 2.7.3.

Please help fix this ASAP, we're using pika in product.

@eandersson
Copy link
Contributor

You need to add channel.confirm_delivery() when setting up the channel.

@qwang2505
Copy link
Author

Thanks! But I got another problem. When I added channel.confirm_delivery(), I got following exception:

Traceback (most recent call last):
File "send.py", line 21, in
connection.close()
File "/home/baina/pikatest/pika/adapters/blocking_connection.py", line 200, in close
self._send_connection_close(reply_code, reply_text)
File "/home/baina/pikatest/pika/connection.py", line 1438, in _send_connection_close
self._on_connection_closed, [spec.Connection.CloseOk])
File "/home/baina/pikatest/pika/connection.py", line 1428, in _rpc
self._send_method(channel_number, method_frame)
File "/home/baina/pikatest/pika/connection.py", line 1492, in _send_method
self._send_frame(frame.Method(channel_number, method_frame))
File "/home/baina/pikatest/pika/adapters/blocking_connection.py", line 396, in _send_frame
super(BlockingConnection, self)._send_frame(frame_value)
File "/home/baina/pikatest/pika/connection.py", line 1479, in _send_frame
self._flush_outbound()
File "/home/baina/pikatest/pika/adapters/blocking_connection.py", line 356, in _flush_outbound
if self._handle_write():
File "/home/baina/pikatest/pika/adapters/base_connection.py", line 338, in _handle_write
total_written += self.socket.send(frame[total_written:])
AttributeError: 'NoneType' object has no attribute 'send'

I'm hoping it would raise a ConnectionError or ChannelError, but why this? Do I miss something? Or it should be like this?

@eandersson
Copy link
Contributor

That might be something worth looking into as I would also expect a proper Exception to be sent. I'll see what I can find out, but Gavin should be able to provide us with a better insight into this.

@qwang2505
Copy link
Author

Thanks a lot! I will handle the AtrributeError in my code, but it should be a proper exception. I will leave this open to track this improper exception problem.

@eandersson
Copy link
Contributor

I'll submit a fix in a couple of minutes.

False
send 1 messages
False
send 2 messages
False
send 3 messages
Traceback (most recent call last):
  File "/home/eandersson/repo/x/t2.py", line 25, in <module>
    connection.close()
  File "/usr/local/lib/python2.7/dist-packages/pika-0.9.13-py2.7.egg/pika/adapters/blocking_connection.py", line 197, in close
    self.process_data_events()
  File "/usr/local/lib/python2.7/dist-packages/pika-0.9.13-py2.7.egg/pika/adapters/blocking_connection.py", line 221, in process_data_events
    raise exceptions.ConnectionClosed()
pika.exceptions.ConnectionClosed

@qwang2505
Copy link
Author

Thanks for help!

gmr pushed a commit that referenced this issue Jun 10, 2013
@William-Kong
Copy link

Was wondering whether this issue has been fixed or not?

I copied the posted code, and added the line "channel.confirm_delivery()", But for me, the basic_publish functions hangs there.

Done think it matters, but my os: Ubuntu 10.04.4 LTS

Thank you in advace

@William-Kong
Copy link

After I hit ctrl-c, I see this:

File "./test1.py", line 21, in
connection.close()
File "/usr/local/lib/python2.6/dist-packages/pika-0.9.14p0-py2.6.egg/pika/adapters/blocking_connection.py", line 211, in close
self.process_data_events()
File "/usr/local/lib/python2.6/dist-packages/pika-0.9.14p0-py2.6.egg/pika/adapters/blocking_connection.py", line 233, in process_data_events
if self._handle_read():
File "/usr/local/lib/python2.6/dist-packages/pika-0.9.14p0-py2.6.egg/pika/adapters/blocking_connection.py", line 345, in _handle_read
if self._read_poller.ready():
File "/usr/local/lib/python2.6/dist-packages/pika-0.9.14p0-py2.6.egg/pika/adapters/blocking_connection.py", line 36, in inner
return f(_args, *_kwargs)
File "/usr/local/lib/python2.6/dist-packages/pika-0.9.14p0-py2.6.egg/pika/adapters/blocking_connection.py", line 78, in ready
events = self.poller.poll(self.poll_timeout)

Thanks

@William-Kong
Copy link

Please ignore the previous post, regarding the ctrl-c, the code was trying to close the connection to the server, which is not accessible anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants