Disconnected From APNS due to Bad Tokens
See original GitHub issueHey folks, I’ve run into this issue during my development. I decided to open an issue just in case others run into the same issue.
I did some testing to see how PyAPNs would react to bad tokens. Let’s say there is a token that is invalid (ie. user may have deleted your application, token hex some how got messed up or cut off, wrong token, a token that doesn’t exist, you accidentally sent a push to a production token etc…).
Let’s make an arbitrary token:
token = 'hecbadd4ed53h45b60f53ed79fef30g11ffddca7abffd0a7f90391ec245fhje'
Great, now that we have our ‘bad’ token. Let’s setup the configuration & payload details.
>>> from apns import *
>>> apns = APNs(use_sandbox=True, cert_file='apns-dev-cert.pem', key_file='apns-dev-key')
>>> payload = Payload(alert = 'hello', sound="default", badge=0)
Next, let’s actually fire off the push notifications.
>>> apns.gateway_server.send_notification(token, payload)
>>> apns.gateway_server.send_notification(token, payload)
>>> apns.gateway_server.send_notification(token, payload)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "apns.py", line 381, in send_notification
self.write(self._get_notification(token_hex, payload))
File "apns.py", line 174, in write
return self._connection().write(string)
File "/usr/lib/python2.7/ssl.py", line 172, in write
return self._sslobj.write(data)
socket.error: [Errno 32] Broken pipe
Notice how it didn’t raise any errors until the 3rd push notification was sent. Now if I send a 4th one after that:
>>> apns.gateway_server.send_notification(token, payload)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "apns.py", line 381, in send_notification
self.write(self._get_notification(token_hex, payload))
File "apns.py", line 174, in write
return self._connection().write(string)
File "/usr/lib/python2.7/ssl.py", line 172, in write
return self._sslobj.write(data)
ssl.SSLError: [Errno 1] _ssl.c:1296: error:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry
It seems I have been disconnected by APNS because of the bad token. Any pushes that I try to send to VALID tokens will not work because the socket object that keeps the connection is gone. I think using a try and except block would be nice somewhere.
Once the exception is raised, the user can then remove the invalid token and then reconnect to APNS.
But the problem here is that it only raises the error after the third Push notification. Is there some way to detect a bad token right off the start? and reconnect? It gets pretty bad especially if you have apns declared as a global variable.
Issue Analytics
- State:
- Created 9 years ago
- Comments:13 (5 by maintainers)
If you catch the exception you can also re-create your apns object
You should probably add some checking to see what the exception was before blindly creating a new connection - but this was a simple fix for us.
I agree, no error notifications when using
send_notification_multiple
. Is there a workaround to this?