Search code examples
c#socketstcpapple-push-notificationspushsharp

Why does my TCP connection with Apple APNS hangs and forcibly disconnects


I'm using a great library PushSharp to send Apple & Android pushnotifications to many client devices. Since last week, we are regularly facing connection issues. Before these issues, no updates or whatsoever have taken place on our hosting environment.

The issue is at this line in ApnsConnection.cs:

stream.AuthenticateAsClient (Configuration.Host, certificates, System.Security.Authentication.SslProtocols.Tls, false);

Sometimes the TCP connection stops responding here. After about 21 minutes the following exception is thrown:

System.IO.IOException: Unable to read data from the connection: 
An existing connection was forcibly closed by the remote host. ---> 
System.Net.Sockets.SocketException: An existing connection was forcibly 
closed by the remote host

The logging from PushSharp displays the following:

13:11:33 | PushServiceV2 | APNS-Client[7]: Sending Batch ID=1, Count=21
13:11:33 | PushServiceV2 | APNS-Client[7]: Connecting (Batch ID=1)
13:11:33 | PushServiceV2 | APNS-Client[7]: client.ConnectAsync
13:11:33 | PushServiceV2 | APNS-Client[7]: client.Client.SetSocketOption
13:11:33 | PushServiceV2 | APNS-Client[7]: SetSocketKeepAliveValues
13:11:33 | PushServiceV2 | APNS-Client[7]: Configuration.SkipSsl:False
13:11:33 | PushServiceV2 | APNS-Client[7]: Create our ssl stream
13:11:33 | PushServiceV2 | APNS-Client[7]: stream.AuthenticateAsClient
13:32:03 | PushServiceV2 | APNS-CLIENT[7]: Send Batch Error: Batch ID=1, Error=System.IO.IOException:

As you can see, it takes 21 minutes before the "connection is forcibly closed by the remote host."

To troubleshoot any issues, I started with checking that after sending of a batch of push notifications I disconnect al TCP connections. Also I increased the time between the batches to 5 minutes. If Apple is disconnecting me because I make to many connections, this should solve it I guess. But it doesn't.

  • 90% of all the batches are successfully sent and received, so the certificate should be correct;
  • I regularly check the Feedback service and remove inactive devices;
  • I have the same issue on a different development machine on a different location/network.

Has anyone any clues or tips on how to further investigate this problem?

Many thanks in advance!


Solution

  • I think we just hit a limit on number of connections. I have read this document: Troubleshooting Push Notifications and took note of this line:

    Another possibility is that you've connected too many times to APNs and further connections have been temporarily blocked. As is documented in the Local and Remote Notification Programming Guide, developers are expected to open a connection and leave it open. If a connection is opened and closed repeatedly, APNs will treat it as a denial of service attack and block connections for a period of time.

    I rewrote my implementation and the usage of PushSharp so that TCP connections are created and closed as little times as possible. It's running for a few days now without any problems. In the logging I see that the connection is only disconnected once each day by apple, and PushSharp nicely reconnects.