* creates a FILE * from the fd passed by the accept thread.
* This operation is potentially expensive (certificate verification),
* so we do it in the child thread context.
+*
+* \note must decrement ref count before returning NULL on error
*/
static void *handle_tls_connection(void *data)
{
if (peer)
X509_free(peer);
fclose(tcptls_session->f);
+ ao2_ref(tcptls_session, -1);
return NULL;
}
}
tcptls_session->client = 0;
+ /* This thread is now the only place that controls the single ref to tcptls_session */
if (ast_pthread_create_detached_background(&launched, NULL, handle_tls_connection, tcptls_session)) {
ast_log(LOG_WARNING, "Unable to launch helper thread: %s\n", strerror(errno));
close(tcptls_session->fd);
__ssl_setup(desc->tls_cfg, 1);
}
- ao2_ref(tcptls_session, +1);
- if (!handle_tls_connection(tcptls_session))
+ /* handle_tls_connection controls the single ref to tcptls_session. If
+ * tcptls_session returns NULL then the session has been destroyed */
+ if (!(tcptls_session = handle_tls_connection(tcptls_session)))
goto error;
return tcptls_session;