adobkin / libcapn

A simple C Library for interact with the Apple Push Notification Service (APNs)
MIT License
100 stars 37 forks source link

socket is never closed when connect() failed. #35

Open matao403 opened 7 years ago

matao403 commented 7 years ago
This issue exists in 1.1.0, 2.0.0 and master.   In __apn_connect function, if connect failed, sock is not set to ctx->sock, application level apn_close() can not close this socket. If application always retry to create connection, it will cause a lot of unclosed socket. Every process used socket is limited, after socket is exhausted, all file/socket operation will be failed. This issue is seen when DNS is ok, but apn traffic is blocked by firewall. 
The fix is easy, add close socket in connect() error branch.

__apn_connect() ... if (connect(sock, (struct sockaddr *) &socket_address, sizeof (socket_address)) < 0) { APN_SET_ERROR(error, APN_ERR_COULD_NOT_INITIALIZE_CONNECTION | APN_ERR_CLASS_INTERNAL, __apn_errors[APN_ERR_COULD_NOT_INITIALIZE_CONNECTION]); APN_RETURN_ERROR; }

    ctx->sock = sock;
matao403 commented 7 years ago

@adobkin Can you have a look at this issue? I have created 2 pull requests in both 1.1.0 and 2.0.0. I'm waiting for your acceptance. If you can accept it, I don't use a patch in local build.