[Asterisk-code-review] tcptls.c: Cleanup TCP/TLS listener thread on abnormal exit. (asterisk[master])

Richard Mudgett asteriskteam at digium.com
Mon Apr 10 18:06:02 CDT 2017


Richard Mudgett has uploaded a new change for review. ( https://gerrit.asterisk.org/5449 )

Change subject: tcptls.c: Cleanup TCP/TLS listener thread on abnormal exit.
......................................................................

tcptls.c: Cleanup TCP/TLS listener thread on abnormal exit.

Temporarily running out of file descriptors should not terminate the
listener thread.  Otherwise, when there becomes more file descriptors
available, nothing is listening.

* Added EMFILE exception to abnormal thread exit.

* Added an abnormal TCP/TLS listener exit error message.

* Closed the TCP/TLS listener socket on abnormal exit so Asterisk does not
appear dead if something tries to connect to the socket.

ASTERISK-26903 #close

Change-Id: I10f2f784065136277f271159f0925927194581b5
---
M main/tcptls.c
1 file changed, 19 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/49/5449/1

diff --git a/main/tcptls.c b/main/tcptls.c
index 1377dd7..f2fe462 100644
--- a/main/tcptls.c
+++ b/main/tcptls.c
@@ -232,9 +232,14 @@
 		}
 		fd = ast_accept(desc->accept_fd, &addr);
 		if (fd < 0) {
-			if ((errno != EAGAIN) && (errno != EWOULDBLOCK) && (errno != EINTR) && (errno != ECONNABORTED)) {
-				ast_log(LOG_ERROR, "Accept failed: %s\n", strerror(errno));
-				break;
+			if (errno != EAGAIN
+				&& errno != EWOULDBLOCK
+				&& errno != EINTR
+				&& errno != ECONNABORTED) {
+				ast_log(LOG_ERROR, "TCP/TLS accept failed: %s\n", strerror(errno));
+				if (errno != EMFILE) {
+					break;
+				}
 			}
 			continue;
 		}
@@ -267,10 +272,20 @@
 
 		/* This thread is now the only place that controls the single ref to tcptls_session */
 		if (ast_pthread_create_detached_background(&launched, NULL, handle_tcptls_connection, tcptls_session)) {
-			ast_log(LOG_ERROR, "Unable to launch helper thread: %s\n", strerror(errno));
+			ast_log(LOG_ERROR, "TCP/TLS unable to launch helper thread: %s\n",
+				strerror(errno));
 			ao2_ref(tcptls_session, -1);
 		}
 	}
+
+	ast_log(LOG_ERROR, "TCP/TLS listener thread ended abnormally\n");
+
+	/* Close the listener socket so Asterisk doesn't appear dead. */
+	fd = desc->accept_fd;
+	desc->accept_fd = -1;
+	if (0 <= fd) {
+		close(fd);
+	}
 	return NULL;
 }
 

-- 
To view, visit https://gerrit.asterisk.org/5449
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I10f2f784065136277f271159f0925927194581b5
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Richard Mudgett <rmudgett at digium.com>



More information about the asterisk-code-review mailing list