Pr0Ger/PyAPNs2

ssl.SSLError: [SSL: BAD_LENGTH] bad length

SearchDream opened this issue · 4 comments

Any one knows the reason for this error, one of my service keep getting this error recently. Thanks in advance.

I'm observing the same error randomly after migrating our AWS ECS services from containers running on EC2 to containers running on Fargate, not sure if that adds to the equation.

Running on Python 3.9. I'm using apns2 indirectly via Django Push Notifications and the exceptions happen while handling Django Channels messages asynchronously (over ASGI on Daphne). It is thus in a high concurrency scenario. I wonder if this would be an issue with either apns2 or the hyper HTTP client (which is unmaintained) not handling concurrency correctly on its use of sockets?

Here's an excerpt of the exception:

SSLError
[SSL: BAD_LENGTH] bad length (_ssl.c:2483)

[...]
 File "/usr/local/lib/python3.9/site-packages/push_notifications/models.py", line 135, in send_message
   r = apns_send_bulk_message(
 File "/usr/local/lib/python3.9/site-packages/push_notifications/apns.py", line 138, in apns_send_bulk_message
   results = _apns_send(
 File "/usr/local/lib/python3.9/site-packages/push_notifications/apns.py", line 87, in _apns_send
   return client.send_notification_batch(
 File "/usr/local/lib/python3.9/site-packages/apns2/client.py", line 211, in send_notification_batch
   result = self.get_notification_result(pending_stream.stream_id)
 File "/usr/local/lib/python3.9/site-packages/apns2/client.py", line 153, in get_notification_result
   with self._connection.get_response(stream_id) as response:
 File "/usr/local/lib/python3.9/site-packages/hyper/http20/connection.py", line 312, in get_response
   return HTTP20Response(stream.getheaders(), stream)
 File "/usr/local/lib/python3.9/site-packages/hyper/http20/stream.py", line 230, in getheaders
   self._recv_cb(stream_id=self.stream_id)
 File "/usr/local/lib/python3.9/site-packages/hyper/http20/connection.py", line 771, in _recv_cb
   self._single_read()
 File "/usr/local/lib/python3.9/site-packages/hyper/http20/connection.py", line 726, in _single_read
   self._send_outstanding_data(tolerate_peer_gone=True, send_empty=False)
 File "/usr/local/lib/python3.9/site-packages/hyper/http20/connection.py", line 466, in _send_outstanding_data
   self._send_cb(data, tolerate_peer_gone=tolerate_peer_gone)
 File "/usr/local/lib/python3.9/site-packages/hyper/http20/connection.py", line 622, in _send_cb
   self._sock.sendall(data)
 File "/usr/local/lib/python3.9/ssl.py", line 1205, in sendall
   v = self.send(byte_view[count:])
 File "/usr/local/lib/python3.9/ssl.py", line 1174, in send
   return self._sslobj.write(data)

I'm also seeing StreamResetError errors which seem related:

[...]
 File "/usr/local/lib/python3.9/site-packages/push_notifications/models.py", line 135, in send_message
   r = apns_send_bulk_message(
 File "/usr/local/lib/python3.9/site-packages/push_notifications/apns.py", line 138, in apns_send_bulk_message
   results = _apns_send(
 File "/usr/local/lib/python3.9/site-packages/push_notifications/apns.py", line 87, in _apns_send
   return client.send_notification_batch(
 File "/usr/local/lib/python3.9/site-packages/apns2/client.py", line 211, in send_notification_batch
   result = self.get_notification_result(pending_stream.stream_id)
 File "/usr/local/lib/python3.9/site-packages/apns2/client.py", line 153, in get_notification_result
   with self._connection.get_response(stream_id) as response:
 File "/usr/local/lib/python3.9/site-packages/hyper/http20/connection.py", line 312, in get_response
   return HTTP20Response(stream.getheaders(), stream)
 File "/usr/local/lib/python3.9/site-packages/hyper/http20/stream.py", line 230, in getheaders
   self._recv_cb(stream_id=self.stream_id)
 File "/usr/local/lib/python3.9/site-packages/hyper/http20/connection.py", line 768, in _recv_cb
   self._get_stream(stream_id)
 File "/usr/local/lib/python3.9/site-packages/hyper/http20/connection.py", line 289, in _get_stream
   raise StreamResetError("Stream forcefully closed")

I found out the issue was caused by the network issues of the VPS.

@SearchDream what did you end up doing to mitigate the issue? did you implement a retry strategy?

I have a retry strategy, but the error still exist. Luckily the network issues was fixed now. Before that I put the push notification service on another VPS provider.