{"action":"closed","issue":{"url":"https://api.github.com/repos/fiorix/txredisapi/issues/82","labels_url":"https://api.github.com/repos/fiorix/txredisapi/issues/82/labels{/name}","comments_url":"https://api.github.com/repos/fiorix/txredisapi/issues/82/comments","events_url":"https://api.github.com/repos/fiorix/txredisapi/issues/82/events","html_url":"https://github.com/fiorix/txredisapi/issues/82","id":91678320,"number":82,"title":"canceling a deferred redis method causes replies to get out of sync","user":{"login":"dpkp","id":843444,"avatar_url":"https://avatars.githubusercontent.com/u/843444?v=3","gravatar_id":"","url":"https://api.github.com/users/dpkp","html_url":"https://github.com/dpkp","followers_url":"https://api.github.com/users/dpkp/followers","following_url":"https://api.github.com/users/dpkp/following{/other_user}","gists_url":"https://api.github.com/users/dpkp/gists{/gist_id}","starred_url":"https://api.github.com/users/dpkp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dpkp/subscriptions","organizations_url":"https://api.github.com/users/dpkp/orgs","repos_url":"https://api.github.com/users/dpkp/repos","events_url":"https://api.github.com/users/dpkp/events{/privacy}","received_events_url":"https://api.github.com/users/dpkp/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-06-29T02:54:06Z","updated_at":"2015-06-30T02:07:07Z","closed_at":"2015-06-30T02:07:07Z","body":"Test code:\r\n```\r\n@defer.inlineCallbacks\r\ndef test_cancel(self):\r\n    db = yield redis.Connection(REDIS_HOST, REDIS_PORT)\r\n\r\n    prefix = 'txredisapi:cancel'\r\n\r\n    # Simple Set + Get\r\n    key = prefix + '1'\r\n    value = 'first'\r\n\r\n    # set should return 'OK'\r\n    res = yield db.set(key, value)\r\n    self.assertEquals('OK', res)\r\n\r\n    # get should return the value\r\n    val = yield db.get(key)\r\n    self.assertEquals(val, value)\r\n\r\n    # Cancel a method, replies get out of sync\r\n    d = db.time()\r\n    d.addErrback(lambda _: True)\r\n    d.cancel()\r\n\r\n    # Set should return 'OK'\r\n    # but note that failure shows it actually returns the result of the cancelled method...\r\n    key = prefix + '2'\r\n    value = 'second'\r\n    res = yield db.set(key, value)\r\n    self.assertEquals('OK', res)\r\n\r\n    # Get should return value\r\n    # but note that it actually returns the result of the previous set method...\r\n    val = yield db.get(key)\r\n    self.assertEquals(val, value)\r\n\r\n    yield db.disconnect()\r\n```\r\n\r\nThe issue appears to be that txredisapi uses a DeferredQueue to manage replies. The redis method returns a deferred from the DeferredQueue, waiting on the next reply to be written by the protocol to the queue. But if the method is cancelled before it consumes its reply from the queue, twisted will remove the deferred from the DeferredQueue waiting list. The reply data, however, is still written to the queue when received and is delivered to the next waiting deferred instead of the deferred that was cancelled and removed from the list. Now all future method calls yield incorrect results.\r\n\r\nSee https://github.com/twisted/twisted/blob/trunk/twisted/internet/defer.py#L1493-L1505"}}