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

那年仲夏 提交于 2019-12-02 02:48:44

When you are trying to write to the socket. If something goes wrong, fwrite will return false or 0 (depending on the php version) as the return value. When it happens, you must manage it.

For the above issue the reason is pretty simple: when SSL_Write returns with SSL_ERROR_WANT_WRITE or SSL_ERROR_WANT_READ, you have to repeat the call to SSL_write with the same parameters again, after the condition is satisfied (read/write available on the socket).

Calling it with different parameters, will yield the 1409F07F bad write retry error.

I get this error many times. To resolve it, just before fwrite(), I added a sleep(5).

Now, every queries are returned with success.

Prashant

Thanks for replying I found similar solutions in google that you should write to socket again. In m case code was throwing exception so I am managing that. So Now I am doing it like this

$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
try {                           
    $result = fwrite($fp, $msg, strlen($msg));
}
catch (Exception $ex) {
    // try once again for socket busy error (fwrite(): SSL operation failed with code 1.
    // OpenSSL Error messages:\nerror:1409F07F:SSL routines:SSL3_WRITE_PENDING)
    sleep(5); //sleep for 5 seconds
    $result = fwrite($fp, $msg, strlen($msg));
    if ($result)
    {
        return true;
    }
    else {
        return false;
    }
}

Now it is not throwing that error in most of the cases. What I found that it happens when socket is busy so I am trying to write again after 5 seconds now. So I guess this should not come again. It is not live yet but in our testing we did not found this issue again. Can you suggest that I am going in right direction? When this code will be live then there will be a huge number of push notifications then in that case will it work fine or is there any other solution in your mind? Thanks for help :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!