question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AlreadyCalledError in twisted connection adapter

See original GitHub issue

Hi!

I am having trouble with the following scenario:

from twisted.internet import defer, protocol, task
import pika
import pika.adapters.twisted_connection


@defer.inlineCallbacks
def test_the_issue(reactor):
    host = "192.168.8.8"
    port = 5672
    client_creator = protocol.ClientCreator(
        reactor,
        pika.adapters.twisted_connection.TwistedProtocolConnection,
        pika.ConnectionParameters(
            host=host,
            port=port,
            virtual_host="/",
            credentials=pika.PlainCredentials("guest", "guest"),
        ),
    )
    connection = yield client_creator.connectTCP(host, port)
    yield connection.ready
    channel = yield connection.channel()

    d = channel.queue_declare("foo")
    d.cancel()
    try:
        yield d
    except defer.CancelledError:
        pass

    yield channel.close()
    yield connection.close()


if __name__ == "__main__":
    task.react(test_the_issue)

It fails every time:

  File "C:\Python27\lib\site-packages\twisted\internet\selectreactor.py", line 149, in _doReadOrWrite
    why = getattr(selectable, method)()
  File "C:\Python27\lib\site-packages\twisted\internet\tcp.py", line 243, in doRead
    return self._dataReceived(data)
  File "C:\Python27\lib\site-packages\twisted\internet\tcp.py", line 249, in _dataReceived
    rval = self.protocol.dataReceived(data)
  File "C:\Python27\lib\site-packages\pika\adapters\twisted_connection.py", line 1209, in dataReceived
    self._impl.data_received(data)
  File "C:\Python27\lib\site-packages\pika\adapters\twisted_connection.py", line 1143, in data_received
    self._on_data_available(data)
  File "C:\Python27\lib\site-packages\pika\connection.py", line 1952, in _on_data_available
    self._process_frame(frame_value)
  File "C:\Python27\lib\site-packages\pika\connection.py", line 2105, in _process_frame
    if self._process_callbacks(frame_value):
  File "C:\Python27\lib\site-packages\pika\connection.py", line 2086, in _process_callbacks
    frame_value)  # Args
  File "C:\Python27\lib\site-packages\pika\callback.py", line 60, in wrapper
    return function(*tuple(args), **kwargs)
  File "C:\Python27\lib\site-packages\pika\callback.py", line 92, in wrapper
    return function(*args, **kwargs)
  File "C:\Python27\lib\site-packages\pika\callback.py", line 233, in process
    callback(*args, **keywords)
  File "C:\Python27\lib\site-packages\pika\channel.py", line 1121, in _on_closeok
    self._transition_to_closed()
  File "C:\Python27\lib\site-packages\pika\channel.py", line 1055, in _transition_to_closed
    self, self, self._closing_reason)
  File "C:\Python27\lib\site-packages\pika\callback.py", line 60, in wrapper
    return function(*tuple(args), **kwargs)
  File "C:\Python27\lib\site-packages\pika\callback.py", line 92, in wrapper
    return function(*args, **kwargs)
  File "C:\Python27\lib\site-packages\pika\callback.py", line 233, in process
    callback(*args, **keywords)
  File "C:\Python27\lib\site-packages\pika\adapters\twisted_connection.py", line 136, in _on_channel_closed
    d.errback(self._closed)
  File "C:\Python27\lib\site-packages\twisted\internet\defer.py", line 501, in errback
    self._startRunCallbacks(fail)
  File "C:\Python27\lib\site-packages\twisted\internet\defer.py", line 560, in _startRunCallbacks
    raise AlreadyCalledError(extra)

Full log with debug messages from pika

Using pika 1.1.0, twisted 19.10.0 and python 2.7.14.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
abompardcommented, Jul 31, 2020

OK, I see where this comes from, and indeed cancelling those deferreds is not supported at the moment. I can make Pika ignore the error, but maybe we can do better. @lukebakken is there a way in Pika to cancel a call that has been made? Say I’m calling queue_declare and the server is taking too long to respond, as a client can I cancel it? If we can’t tell the server to cancel the operation, maybe as a client we can remove the call from the RPC buffer and not wait for the replies? I’m not sure what’s best to do.

0reactions
lukebakkencommented, Jun 29, 2022

I will investigate this before the next major release, thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

AlreadyCalledError in twisted connection adapter - Google Groups
Hi! I am having trouble with the following scenario: from twisted.internet import defer, protocol, reactor. import pika.adapters ...
Read more >
Twisted Connection Adapter — pika 1.2.1 documentation
A Pika-specific implementation of a Twisted Protocol. Allows using Twisted's non-blocking connectTCP/connectSSL methods for connecting to the server.
Read more >
Developers - AlreadyCalledError in twisted connection adapter -
Hi! I am having trouble with the following scenario: from twisted.internet import defer, protocol, task import pika import pika.adapters.twisted_connection ...
Read more >
Deferred Reference — Twisted 18.4.0 documentation
If you call it multiple times, you will still get an AlreadyCalledError exception. ... Cancellation is best achieved by closing the connection.
Read more >
Twisted Documentation - Read the Docs
disconnection would be to call connector.connect() when the connection is lost: from twisted.internet.protocol import ClientFactory.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found