Error 116 Occurs after 12 hours of TLS socket connect()

Hello DevZone community,

I have been using the TLS socket in my application, which is executed whenever we need to call API or send logs.
However, I'm encountering an error after exactly 12 hours of operation. The error code returned is 116.

int mc_api_call(char* pReqMsg, int Msglen, char* pHost, int HostLen, char* pRcvBuf, int RcvBuflen)
{
	int err;
	int fd;
	int ret = -1;
	int sendbyte, recvbyte;
	size_t off;
	struct addrinfo *res;
	struct addrinfo hints = {
		.ai_family = AF_INET,
		.ai_socktype = SOCK_STREAM,
	};

	k_sem_take(&mc_enable, K_FOREVER); 

	err = getaddrinfo(pHost, NULL, &hints, &res);
	if (err) {
		MLOG_ERROR("getaddrinfo() failed, err %d", errno);

		k_sem_give(&mc_enable);		

		return ret;
	}
#if 0
	((struct sockaddr_in *)res->ai_addr)->sin_port = htons(HTTP_PORT);
#else
	((struct sockaddr_in *)res->ai_addr)->sin_port = htons(HTTPS_PORT);
#endif

#if 0
	fd = socket(AF_INET, SOCK_STREAM, res->ai_protocol);
#else
	fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TLS_1_2);
#endif
	if (fd == -1) {
		// LOG_INF("Failed to open socket!");
		goto clean_up;
	}
	err = tls_setup(fd, pHost, HostLen);
	if (err) {
		goto clean_up;
	}

	MLOG_DEBUG("Connecting to %s", pHost);
	err = connect(fd, res->ai_addr, sizeof(struct sockaddr_in));
	if (err) {
		MLOG_ERROR("connect() failed, err: %d", errno);
		goto clean_up;
	}

	off = 0;
	do {
		sendbyte = send(fd, pReqMsg+off, Msglen - off, 0);
		if (sendbyte < 0) {
			MLOG_ERROR("send() failed, err %d", errno);
			goto clean_up;
		}
		off += sendbyte;
	} while (off < sendbyte);
	MLOG_DEBUG("Send %d bytes", off);
	off = 0;

	memset(pRcvBuf, 0x00, RcvBuflen);
#if 0
	do{
		recvbyte = recv(fd, pRcvBuf+off, RcvBuflen - off, MSG_DONTWAIT);
		MLOG_DEBUG("Received %d bytes", recvbyte);
		if (recvbyte == 0) {
			break;
		}
		cnt++;
		if(recvbyte < 0)
		{
			if(cnt > 10)
			{
				MLOG_WARNING("%s", "Receive time out");
				break;
			}
			else
			{
				k_sleep(K_MSEC(1000));
				continue;
			}
		}
		else
		{
			cnt = 0;
		}
		off += recvbyte;
	} while (recvbyte < RcvBuflen /* peer closed connection */);
#else
	do {
		recvbyte = recv(fd, pRcvBuf+off, RcvBuflen - off, 0);
		MLOG_DEBUG("Received %d bytes", recvbyte);
		if (recvbyte < 0) {
			break;
		}
		off += recvbyte;
	} while (recvbyte != 0 /* peer closed connection */);
#endif

	MLOG_DEBUG("Received end %d bytes", off);
	ret = off;

clean_up:
	freeaddrinfo(res);
	(void)close(fd);

	k_sem_give(&mc_enable);
	return ret;
}


My Environment

NCS - SDK 1.9.1
Device - nrf9160 customized one

Error log

[2023/05/17 23:42:44] [my_thread] [ERROR] connect() failed, err: 116

I'm seeking assistance in understanding the possible causes of this error and any recommended solutions or workarounds to resolve it.
Any insights or experiences shared would be greatly appreciated.

Thank you in advance for your help!

  • We are short staffed this week due to Public Holidays in Norway. We will be back on Monday 22nd and hope to be able to answer all incoming requests within a couple of days, depending on the backlog. I am sorry for the inconvenience.

    Best regards,

    Vidar

  • Hi,

    Could you share the full log of the device?

    How often do you send data?

    -116 is ETIMEDOUT, which in this case probably means that the server didn't reply to connection request for some reason.

    Does the same happen when you do not use TLS?

    What SIM card are you using, and what network are you connected to?

    Could it be that the operator limits how long the device can stay connected and send data?

    Best regards,

    Didrik

  • Hello Didrik,

    Thank you for your replay and time.

    Can you please share the full logs of your device?
    ⇒ Here it is the log file

    [2023/05/16 23:42:26] [lte_thread] [ERROR] lte_lc_conn_eval_params_get error 4
    [2023/05/16 23:42:44] [sensor_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:42:44] [sensor_thre] [ERROR] Error while sending log file 20230517_084233_830RX030017.log to AWS S3 -1
    [2023/05/16 23:42:56] [mloca_threa] [ERROR] connect() failed, err: 116
    [2023/05/16 23:42:56] [mloca_threa] [ERROR] Error while sending log file 20230517_084244_830RX030017.log to AWS S3 -1
    [2023/05/16 23:43:06] [mloca_threa] [ERROR] connect() failed, err: 116
    [2023/05/16 23:43:06] [mloca_threa] [ERROR] [mc_api_license_token_get]:AccessToken_api_call Error=-1
    [2023/05/16 23:43:18] [mloca_threa] [ERROR] connect() failed, err: 116
    [2023/05/16 23:43:18] [mloca_threa] [ERROR] Error while sending log file 20230517_084306_830RX030017.log to AWS S3 -1
    [2023/05/16 23:43:18] [mloca_threa] [ERROR] Periodic Api error
    [2023/05/16 23:44:03] [sensor_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:44:03] [sensor_thre] [ERROR] Error while sending log file 20230517_084353_830RX030017.log to AWS S3 -1
    [2023/05/16 23:45:22] [sensor_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:45:22] [sensor_thre] [ERROR] Error while sending log file 20230517_084512_830RX030017.log to AWS S3 -1
    ========== sector=b000 ==========
    [2023/05/16 23:46:30] [sysworkq] [ERROR] connect() failed, err: 116
    [2023/05/16 23:46:30] [sysworkq] [ERROR] Error while sending log file 20230517_084620_830RX030017.log to AWS S3 -1
    [2023/05/16 23:46:40] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:46:41] [update_thre] [ERROR] Error while sending log file 20230517_084630_830RX030017.log to AWS S3 -1
    [2023/05/16 23:46:51] [sensor_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:46:51] [sensor_thre] [ERROR] Error while sending log file 20230517_084641_830RX030017.log to AWS S3 -1
    [2023/05/16 23:47:01] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:47:11] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:47:21] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:47:21] [update_thre] [ERROR] Error while sending log file 20230517_084711_830RX030017.log to AWS S3 -1
    [2023/05/16 23:47:31] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:47:31] [update_thre] [ERROR] [ApiSetOta_AcsToken_Get]:AccessToken_api_call Error: -1
    [2023/05/16 23:47:42] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:47:42] [update_thre] [ERROR] Error while sending log file 20230517_084731_830RX030017.log to AWS S3 -1
    [2023/05/16 23:47:52] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:47:52] [update_thre] [ERROR] Error while sending log file 20230517_084742_830RX030017.log to AWS S3 -1
    [2023/05/16 23:48:02] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:48:02] [update_thre] [ERROR] Error while sending log file 20230517_084752_830RX030017.log to AWS S3 -1
    [2023/05/16 23:48:12] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:48:12] [update_thre] [ERROR] [ApiSetOta_AcsToken_Get]:AccessToken_api_call Error: -1
    [2023/05/16 23:48:22] [sensor_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:48:22] [sensor_thre] [ERROR] Error while sending log file 20230517_084802_830RX030017.log to AWS S3 -1
    [2023/05/16 23:48:34] [mloca_threa] [ERROR] connect() failed, err: 116
    [2023/05/16 23:48:34] [mloca_threa] [ERROR] Error while sending log file 20230517_084822_830RX030017.log to AWS S3 -1
    ========== sector=c000 ==========
    [2023/05/16 23:48:45] [mloca_threa] [ERROR] connect() failed, err: 116
    [2023/05/16 23:48:45] [mloca_threa] [ERROR] [mc_api_license_token_get]:AccessToken_api_call Error=-1
    [2023/05/16 23:48:57] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:48:57] [update_thre] [ERROR] Error while sending log file 20230517_084834_830RX030017.log to AWS S3 -1
    [2023/05/16 23:49:07] [mloca_threa] [ERROR] connect() failed, err: 116
    [2023/05/16 23:49:07] [mloca_threa] [ERROR] Error while sending log file 20230517_084857_830RX030017.log to AWS S3 -1
    [2023/05/16 23:49:07] [mloca_threa] [ERROR] Periodic Api error
    [2023/05/16 23:49:17] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:49:17] [update_thre] [ERROR] Error while sending log file 20230517_084907_830RX030017.log to AWS S3 -1
    [2023/05/16 23:49:28] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:49:28] [update_thre] [ERROR] Error while sending log file 20230517_084917_830RX030017.log to AWS S3 -1
    [2023/05/16 23:49:38] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:49:38] [update_thre] [ERROR] [ApiSetOta_AcsToken_Get]:AccessToken_api_call Error: -1
    [2023/05/16 23:49:48] [sensor_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:49:48] [sensor_thre] [ERROR] Error while sending log file 20230517_084931_830RX030017.log to AWS S3 -1
    [2023/05/16 23:49:58] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:49:58] [update_thre] [ERROR] Error while sending log file 20230517_084948_830RX030017.log to AWS S3 -1
    [2023/05/16 23:50:08] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:50:08] [update_thre] [ERROR] Error while sending log file 20230517_084958_830RX030017.log to AWS S3 -1
    [2023/05/16 23:50:18] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:50:18] [update_thre] [ERROR] Error while sending log file 20230517_085008_830RX030017.log to AWS S3 -1
    [2023/05/16 23:50:29] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:50:29] [update_thre] [ERROR] [ApiSetOta_AcsToken_Get]:AccessToken_api_call Error: -1
    [2023/05/16 23:50:39] [update_thre] [ERROR] connect() failed, err: 116
    ========== sector=d000 ==========
    [2023/05/16 23:50:39] [update_thre] [ERROR] Error while sending log file 20230517_085029_830RX030017.log to AWS S3 -1
    [2023/05/16 23:50:49] [update_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:50:49] [update_thre] [ERROR] Error while sending log file 20230517_085039_830RX030017.log to AWS S3 -1
    [2023/05/16 23:51:07] [sensor_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:51:07] [sensor_thre] [ERROR] Error while sending log file 20230517_085056_830RX030017.log to AWS S3 -1
    [2023/05/16 23:52:26] [sensor_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:52:26] [sensor_thre] [ERROR] Error while sending log file 20230517_085215_830RX030017.log to AWS S3 -1
    [2023/05/16 23:53:44] [sensor_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:53:44] [sensor_thre] [ERROR] Error while sending log file 20230517_085334_830RX030017.log to AWS S3 -1
    [2023/05/16 23:54:18] [mloca_threa] [ERROR] connect() failed, err: 116
    [2023/05/16 23:54:18] [mloca_threa] [ERROR] Error while sending log file 20230517_085407_830RX030017.log to AWS S3 -1
    [2023/05/16 23:54:30] [mloca_threa] [ERROR] connect() failed, err: 116
    [2023/05/16 23:54:30] [mloca_threa] [ERROR] [mc_api_license_token_get]:AccessToken_api_call Error=-1
    [2023/05/16 23:54:40] [mloca_threa] [ERROR] connect() failed, err: 116
    [2023/05/16 23:54:40] [mloca_threa] [ERROR] Error while sending log file 20230517_085430_830RX030017.log to AWS S3 -1
    [2023/05/16 23:54:40] [mloca_threa] [ERROR] Periodic Api error
    [2023/05/16 23:55:03] [sensor_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:55:03] [sensor_thre] [ERROR] Error while sending log file 20230517_085453_830RX030017.log to AWS S3 -1
    [2023/05/16 23:56:22] [sensor_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:56:22] [sensor_thre] [ERROR] Error while sending log file 20230517_085612_830RX030017.log to AWS S3 -1
    [2023/05/16 23:57:41] [sensor_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:57:41] [sensor_thre] [ERROR] Error while sending log file 20230517_085731_830RX030017.log to AWS S3 -1
    [2023/05/16 23:59:00] [sensor_thre] [ERROR] connect() failed, err: 116
    [2023/05/16 23:59:00] [sensor_thre] [ERROR] Error while sending log file 20230517_085850_830RX030017.log to AWS S3 -1
    ========== sector=e000 ==========
    

     

    How often do you send data?

    ⇒ most often has a one-second period.

     

    Does the same happen when you do not use TLS?

    ⇒ TLS is a mandatory requirement due to the communication specifications of server.

     

    What SIM card are you using, and what network are you connected to?

    ⇒ The manufacturer of  SIM is "IIJ Global" from Japan, which is an e-SIM.

     

    Could it be that the operator limits how long the device can stay connected and send data?
    ⇒  We are not sure IIJ Global operator limit at present. We ll contact IIJ.

    Best Regards

    Praveen Deshmane

  • Hello Didrik,

    can you please check and reply.

    Best Regards

    Praveen Deshmane

  • Hi, and sorry for the late reply.

    Praveen Deshmane said:
    Can you please share the full logs of your device?
    ⇒ Here it is the log file

    Do you have anything of what happens before you start to get errors?

    Are you able to take a modem trace?

Related