djacobs / PyAPNs

Python library for interacting with the Apple Push Notification service (APNs)
http://pypi.python.org/pypi/apns/
MIT License
1.22k stars 377 forks source link

Remote Socket Error #116

Open C6silver opened 9 years ago

C6silver commented 9 years ago

I am running across the issue below rather frequently. The circumstances are not 100% reproducible but it happens mostly when there is some delay in sending APNs. For example, I may send an APN and not send one again for 5 minutes or more and the error will occur (again not all the time). However, I have also seen it in the middle of sending individual APN events so it is not just a case of this happening after some period of downtime. But it does seem to happen with greater frequency where there is some delay. These are being sent in enhanced mode (no frames). Of final interest is that the APN does appear to be sent anyway:

sending notification with id:0 to APNS failed: <class 'google.appengine.api.remote_socket._remote_socket_error.error'>: [Errno 22] Invalid argument in 1th attempt, will wait 10 secs for next action Traceback (most recent call last): File "/base/data/home/apps/s~browersr-ios/2.383208000261957988/apns.py", line 533, in sendnotification self.write(message) File "/base/data/home/apps/s~browersr-ios/2.383208000261957988/apns.py", line 263, in write , wlist, _ = select.select([], [self._connection()], [], WAIT_WRITE_TIMEOUT_SEC) File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/remote_socket/_remote_socket.py", line 493, in select raise _SystemExceptionFromAppError(e) error: [Errno 22] Invalid argument

exception occur when reading APNS error-response: <class 'google.appengine.api.remote_socket._remote_socketerror.error'>: [Errno 22] Invalid argument Traceback (most recent call last): File "/base/data/home/apps/s~browersr-ios/2.383208000261957988/apns.py", line 600, in run rlist, , _ = select.select([self._apns_connection._connection()], [], [], WAIT_READ_TIMEOUT_SEC) File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/remote_socket/_remote_socket.py", line 493, in select raise _SystemExceptionFromAppError(e) error: [Errno 22] Invalid argument

C6silver commented 9 years ago

This error continues to happen frequently. Essentially if you wait x minutes (hard to know exactly how long but let's say 5 or more minutes) between sending APN's and the error will occur. There has no case where I have come back say after 20 minutes and have not run into this problem. Again, the APN is still sent but of course there is a delay and the error above in the log. Any ideas?

jimhorng commented 9 years ago

@C6silver , Seems that google app engine have some restriction on socket with its own implementation (remote_socket), please see app engine sockets. Maybe you can try the same code on local with your python's standard socket and see if the problem still exists?

C6silver commented 9 years ago

@jimhorng Based on your understanding what restriction do you think would be the cause of this error? I am on a paid app and the APNs are sent. It is clear that the process works but I am wondering if there is some kind of time out occurring? I am not seeing a restriction that would yield this kind of error. The only timing based one I can see is: "Sockets may be reclaimed after 2 minutes of inactivity; any socket operation keeps the socket alive for a further 2 minutes. " However, that doesn't seem like it would cause this problem.

C6silver commented 9 years ago

@jimhorng Some progress. You will recall that we moved the instantiation of the APNs object out of my class and to the module level due to the issue of token sends stopping when a token was bad. The idea was that the class exited before things finished. That move did solve that problem but it appears to have caused the issue we are seeing in this thread. When I moved the "APNs(use_sandbox=True,cert_file="cert.pem", key_file="key.pem", enhanced=True)" back to the class, I stopped getting the error above. However, of course that has introduced the other problem again which is tokens not sending after a bad one is encountered.

dexity commented 9 years ago

I was also having this issue when attached the created APNs object to the flask application object. I solved the issue when I instantiated the APNs object right before it's used. Still have to figure out the performance consequences of the solution.

jimhorng commented 9 years ago

@C6silver , @dexity , thanks for finding out the problem, may you paste the usage code here? (just mask out confidential part :) ), including 1) how APNs object is instantiated and 2) how it was used and 3) how program is terminated Thanks :)