{"action":"created","issue":{"url":"https://api.github.com/repos/samuraisam/pyapns/issues/33","labels_url":"https://api.github.com/repos/samuraisam/pyapns/issues/33/labels{/name}","comments_url":"https://api.github.com/repos/samuraisam/pyapns/issues/33/comments","events_url":"https://api.github.com/repos/samuraisam/pyapns/issues/33/events","html_url":"https://github.com/samuraisam/pyapns/issues/33","id":4904225,"number":33,"title":"Fault 500: 'Connection to the APNS server could not be made.'","user":{"login":"aht","id":129813,"avatar_url":"https://avatars.githubusercontent.com/u/129813?v=3","gravatar_id":"","url":"https://api.github.com/users/aht","html_url":"https://github.com/aht","followers_url":"https://api.github.com/users/aht/followers","following_url":"https://api.github.com/users/aht/following{/other_user}","gists_url":"https://api.github.com/users/aht/gists{/gist_id}","starred_url":"https://api.github.com/users/aht/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aht/subscriptions","organizations_url":"https://api.github.com/users/aht/orgs","repos_url":"https://api.github.com/users/aht/repos","events_url":"https://api.github.com/users/aht/events{/privacy}","received_events_url":"https://api.github.com/users/aht/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2012-06-05T08:29:57Z","updated_at":"2015-07-07T17:45:18Z","closed_at":null,"body":"The XML-RPC client got error \\<Fault 500: 'Connection to the APNS server could not be made.'\\>\n\nServer logs:\n\n    2012-06-05 04:45:03+0000 [Uninitialized] \"errback in apns-service-write : [Failure instance: Traceback: <type 'exceptions.AttributeError'>: 'NoneType' object h\n    as no attribute 'sendMessage'\\n/opt/lib/python2.7/site-packages/pyapns/server.py:74:connectionMade\\n/opt/lib/python2.7/site-packages/pyapns/server.py:141:addCl\n    ient\\n/opt/lib/python2.7/site-packages/twisted/internet/defer.py:368:callback\\n/opt/lib/python2.7/site-packages/twisted/internet/defer.py:464:_startRunCallback\n    s\\n--- <exception caught here> ---\\n/opt/lib/python2.7/site-packages/twisted/internet/defer.py:551:_runCallbacks\\n/opt/lib/python2.7/site-packages/pyapns/serve\n    r.py:210:<lambda>\\n]\"\n    2012-06-05 04:45:03+0000 [HTTPChannel,388658,50.18.191.88] 50.18.191.88 - - [05/Jun/2012:04:45:03 +0000] \"POST / HTTP/1.1\" 500 7058 \"-\" \"xmlrpclib.py/1.0.1 (by\n     www.pythonware.com)\"\n    2012-06-05 04:45:03+0000 [HTTPChannel,388659,50.18.191.88] Unhandled Error\n            Traceback (most recent call last):\n              File \"/opt/lib/python2.7/site-packages/twisted/web/http.py\", line 1349, in dataReceived\n                finishCallback(data[contentLength:])\n              File \"/opt/lib/python2.7/site-packages/twisted/web/http.py\", line 1563, in _finishRequestBody\n                self.allContentReceived()\n              File \"/opt/lib/python2.7/site-packages/twisted/web/http.py\", line 1618, in allContentReceived\n                req.requestReceived(command, path, version)\n              File \"/opt/lib/python2.7/site-packages/twisted/web/http.py\", line 773, in requestReceived\n                self.process()\n            --- <exception caught here> ---\n              File \"/opt/lib/python2.7/site-packages/twisted/web/server.py\", line 132, in process\n                self.render(resrc)\n              File \"/opt/lib/python2.7/site-packages/twisted/web/server.py\", line 167, in render\n                body = resrc.render(self)\n              File \"/opt/lib/python2.7/site-packages/twisted/web/resource.py\", line 216, in render\n                return m(request)\n              File \"/opt/lib/python2.7/site-packages/twisted/web/xmlrpc.py\", line 160, in render_POST\n                function = self.lookupProcedure(functionPath)\n              File \"/opt/lib/python2.7/site-packages/twisted/web/xmlrpc.py\", line 232, in lookupProcedure\n                handler = self.getSubHandler(prefix)\n              File \"/opt/lib/python2.7/site-packages/twisted/web/xmlrpc.py\", line 140, in getSubHandler\n                return self.subHandlers.get(prefix, None)\n            exceptions.AttributeError: APNSServer instance has no attribute 'subHandlers'\n            \n    2012-06-05 04:45:03+0000 [HTTPChannel,388659,50.18.191.88] 50.18.191.88 - - [05/Jun/2012:04:45:03 +0000] \"POST / HTTP/1.1\" 500 7058 \"-\" \"xmlrpclib.py/1.0.1 (by www.pythonware.com)\"\n\nI suspect the relevant piece is <type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'sendMessage'"},"comment":{"url":"https://api.github.com/repos/samuraisam/pyapns/issues/comments/119280615","html_url":"https://github.com/samuraisam/pyapns/issues/33#issuecomment-119280615","issue_url":"https://api.github.com/repos/samuraisam/pyapns/issues/33","id":119280615,"user":{"login":"dkrol-basis","id":11843050,"avatar_url":"https://avatars.githubusercontent.com/u/11843050?v=3","gravatar_id":"","url":"https://api.github.com/users/dkrol-basis","html_url":"https://github.com/dkrol-basis","followers_url":"https://api.github.com/users/dkrol-basis/followers","following_url":"https://api.github.com/users/dkrol-basis/following{/other_user}","gists_url":"https://api.github.com/users/dkrol-basis/gists{/gist_id}","starred_url":"https://api.github.com/users/dkrol-basis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dkrol-basis/subscriptions","organizations_url":"https://api.github.com/users/dkrol-basis/orgs","repos_url":"https://api.github.com/users/dkrol-basis/repos","events_url":"https://api.github.com/users/dkrol-basis/events{/privacy}","received_events_url":"https://api.github.com/users/dkrol-basis/received_events","type":"User","site_admin":false},"created_at":"2015-07-07T17:45:18Z","updated_at":"2015-07-07T17:45:18Z","body":"I dug in a bit and figured out the following:\r\n\r\nWhen you register a callback with Twisted, the value passed into your callback function is the return value of the previously registered callback function. The exception of course is the first callback, which gets its argument from the call to `.callback`. In our case it passes a client object to `.callback` in the `addClient` method. See [here](https://twistedmatrix.com/documents/14.0.0/core/howto/defer.html) for details on all that.\r\n\r\nIn our case the callback function is a lambda which calls `sendMessage` but doesn't return anything, which in Python of course defaults to `None`. My suspicion is that there is a race condition, causing the `addCallback` function to be called multiple times before being fired off by `.callback`. It's fired off in the `addClient` function, which also sets `clientProtocol`. With `clientProtocol` set, the `addCallback` logic is skipped in subsequent calls to `write`, and instead the `sendMessage` is called directly.\r\n\r\nI think the idea here is that one message can be sent to the server and queued up to be executed when we finally have a client established. What was not considered is the possibility that two or more messages can be similarly queued. Or if it was considered, it wasn't handled properly.\r\n\r\nSo, bottom line, what needs to happen is for the lambda to be replaced with a function that does the same, but also returns the same client that was passed in."}}