Just recently we were working on some updates for our hosted twilio application service and ran into some issues with a larger volume of call traffic (32K/mo).
GnuTLS Errors from Twilio
This is a snip of the error that was being spewed out from the cURL library.
56:GnuTLS recv error (-9): A TLS packet with unexpected length was received.
These issues only occurred when paging backwards through the records, and only after few requests have been issues. That is, paging backwards by 1024 records we saw the errors round pages 30 (of 80+). There was no consistent behaviour.
Naturally we contacted the Twilio support team (which is pretty good!) And we through few steps with them.
These issues would still occur when we tried page sizes of 512 and 256 and 128. Other attempts at resolution we tried were using different versions of SSL (SSLv2, SSLv3, TLSv1). All the requests were throwing this error in-side of the 30s timeout that is implemented on the Twilio API.
The actual fix for us was a two part process. Firstly, we added a retry loop on our Twilio requests (as suggested by Twilio support).
Also, very important when using the Twilio APIs and paging back through the records is to use the "next_uri" returned in the response. There is a special parameter there AfterSid which provides a good performance hint for the Twilio system.