Răsfoiți Sursa

Fix race condition in _dns_client_close_socket_ext

Nick Peng 6 zile în urmă
părinte
comite
9929ddd098
2 a modificat fișierele cu 10 adăugiri și 10 ștergeri
  1. 1 1
      src/dns_client/client_http2.c
  2. 9 9
      src/dns_client/client_socket.c

+ 1 - 1
src/dns_client/client_http2.c

@@ -478,7 +478,7 @@ static int _dns_client_http2_process_read(struct dns_server_info *server_info)
 		http2_ctx_put(http2_ctx);
 		return 0;
 	} else if (ret < 0) {
-		tlog(TLOG_ERROR, "http2 handshake failed.");
+		tlog(TLOG_DEBUG, "http2 handshake failed.");
 		http2_ctx_put(http2_ctx);
 		return -1;
 	}

+ 9 - 9
src/dns_client/client_socket.c

@@ -88,16 +88,13 @@ int _dns_client_create_socket(struct dns_server_info *server_info)
 void _dns_client_close_socket_ext(struct dns_server_info *server_info, int no_del_conn_list)
 {
 	dns_server_status server_status = DNS_SERVER_STATUS_DISCONNECTED;
+	int saved_fd;
 
 	pthread_mutex_lock(&server_info->lock);
+	saved_fd = server_info->fd;
 	server_status = server_info->status;
 	server_info->status = DNS_SERVER_STATUS_DISCONNECTED;
 
-	/* remove fd from epoll */
-	if (server_info->fd > 0) {
-		epoll_ctl(client.epoll_fd, EPOLL_CTL_DEL, server_info->fd, NULL);
-	}
-
 	if (server_info->ssl) {
 		/* Shutdown ssl */
 		if (server_status == DNS_SERVER_STATUS_CONNECTED) {
@@ -166,15 +163,18 @@ void _dns_client_close_socket_ext(struct dns_server_info *server_info, int no_de
 	if (server_info->proxy) {
 		proxy_conn_free(server_info->proxy);
 		server_info->proxy = NULL;
-	} else if (server_info->fd > 0) {
-		close(server_info->fd);
 	}
 
-	if (server_info->fd > 0) {
+	/* Close fd and remove from epoll */
+	if (saved_fd > 0) {
+		if (server_info->proxy == NULL) {
+			close(saved_fd);
+		}
+		server_info->fd = -1;
+		epoll_ctl(client.epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
 		tlog(TLOG_DEBUG, "server %s:%d closed.", server_info->ip, server_info->port);
 	}
 
-	server_info->fd = -1;
 	/* update send recv time */
 	time(&server_info->last_send);
 	time(&server_info->last_recv);