mac-cain13 / notificato

Takes care of Apple push notifications (APNS) in your PHP projects.
MIT License
224 stars 44 forks source link

SSL3_WRITE_PENDING warnings while sending lot of push-alerts #41

Closed chekalsky closed 9 years ago

chekalsky commented 9 years ago

WARNING

fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry

/Wrep/Notificato/Apns/Gateway.php at line 107. {"messageEnvelope":"[object](Wrep\Notificato\Apns\MessageEnvelope: {})","binaryMessage":null,"bytesSend":0,"retryMessageEnvelope":"[object](Wrep\Notificato\Apns\MessageEnvelope: {})"}

chekalsky commented 9 years ago
$notificato = new Notificato();

$certificateProd = $notificato->createCertificate($certDir . '/xxx.cer');
$certificateDev  = $notificato->createCertificate($certDir . '/yyy.cer');

// Build the base alert
$builder = $notificato->messageBuilder()
    ->setExpiresAt(new DateTime('+12 hour'))
    ->setAlert($alertText)
    ->setSound(($isSound) ? 'default' : null)
    ->setPayload($customParams)
    ->setBadge($badgeCount);

// Adding alerts to queue
foreach ($tokens as $deviceToken) {
    $builder->setDeviceToken($deviceToken['token']);

    if ($deviceToken['isDeveloper'] != 1) {
        $build = $builder->setCertificate($certificateProd)->build();
    } else {
        $build = $builder->setCertificate($certificateDev)->build();
    }

    $notificato->queue($build);
}

// Sending
$notificato->flush();
mac-cain13 commented 9 years ago

Thanks for reporting your problem.

I'm wondering if you have some more information on when these warnings happen. Do they already happen when you call flush for the first time? Or do they only show up on later batches? Could you also tell me what PHP and OpenSSL version you are using?

Could have something to do with Apple closing the connection due inactivity or due SSL weirdness where we have to do a retry.

chekalsky commented 9 years ago

PHP 5.4.34-0+deb7u1 OpenSSL 1.0.1e

It is rare problem, not every time. But possibility of meeting this warning increases when I add to $tokens more items (etc more than 100). But sometimes everything is OK.

I am not sure if it is happens for first flush or not, it is running in background in gearman. But I think there is possibility of that is happens on second and third flush().

chekalsky commented 9 years ago

Just now got this error once again.

It is happening in the endless cycle until php's timeout. It is put failed message into queue again and again and again, but the strange thing is that messages are duplicated on device and coming over and over again.

I think you should create some emergency plan for this scenario or solve the reason of error in fwrite().

P.S. I will move to another php library, I hope my report helps.

UPD. Ugh, there is no other cool libraries on github for push alerts, please, could you fix this issue? :-)

chekalsky commented 9 years ago

look at this:

http://apple.stackexchange.com/questions/23470/apple-push-notifications-ssl-error http://stackoverflow.com/questions/18378534/apns-ssl-operation-failed-with-code-1 http://stackoverflow.com/questions/12156976/apple-push-notification-php-ssl-operation-failed-with-code-1

mac-cain13 commented 9 years ago

Thanks for the additional info! I found stack overflow posts like the one you mention here indeed, seems like we have to handle SSL write errors better. The hard thing is that I don't have a good reproduction path yet. :(

I think handling these errors by reconnecting would maybe already fix it. I'll take a look. :)

mac-cain13 commented 9 years ago

Ok, made some changes! Could you test the develop branch by changing your composer file? Would be great to know if this fixes the issue for you.

You can do this by changing the composer line to: "wrep/notificato": "dev-development"

What was happening:

  1. Message failed to send due SSL error
  2. Message re-queued because send failed
  3. Check if Apple tells us about an error
  4. Continue sending queued messages

What I've changed it to:

  1. Message failed to send due SSL error
  2. Message re-queued because send failed
  3. Check if Apple tells us about an error
  4. Close socket
  5. Reconnect and start sending procedure
chekalsky commented 9 years ago

Ok, and I'll increase count of notifications sending through one connection to increase possibility of error :)

chekalsky commented 9 years ago

Well, it works but I think we should wait couple of days to make this thing right.

mac-cain13 commented 9 years ago

Great, thanks for testing! I'll wait a few days and if I don't hear of any errors from you I will release a new version.

Keep me posted about the results of this new version. :)

mac-cain13 commented 9 years ago

@chekalskiy How does it look? Everything still running stable? Have you seen any of these SSL warnings so far?

chekalsky commented 9 years ago

I am little away now but I 99% sure that everything is OK. I can tell more after next Tuesday.

mac-cain13 commented 9 years ago

Good to know! Thanks.

I'll create an official release asap, let me know if you experience any issues.

chekalsky commented 9 years ago

:+1:

chekalsky commented 9 years ago

Did you forget about new release?

mac-cain13 commented 9 years ago

I did not forget, but thanks for the reminder. :) Version 1.1.1 with this fixed is now available.