Skip to content

Commit 960480e

Browse files
committed
http2: fix memory leak issue.
1 parent 4de6963 commit 960480e

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

src/dns_client/client_http2.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ static int _dns_client_http2_pending_data(struct dns_conn_stream *stream, struct
182182
struct dns_query_struct *query, void *packet, int len)
183183
{
184184
struct epoll_event event;
185+
186+
/* Validate input parameters */
187+
if (len <= 0 || len > DNS_IN_PACKSIZE - 128) {
188+
errno = EINVAL;
189+
return -1;
190+
}
191+
185192
if (DNS_TCP_BUFFER - stream->send_buff.len < len) {
186193
errno = ENOMEM;
187194
return -1;
@@ -450,7 +457,7 @@ static int _dns_client_http2_process_read(struct dns_server_info *server_info)
450457
struct http2_poll_item poll_items[128];
451458
int poll_count = 0;
452459
int loop_count = 0;
453-
const int MAX_LOOP_COUNT = 128;
460+
const int MAX_LOOP_COUNT = 512;
454461
struct dns_conn_stream *conn_stream = NULL;
455462
int ret = 0;
456463
int i = 0;

src/dns_server/connection.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ void _dns_server_conn_release(struct dns_server_conn_head *conn)
6262
SSL_free(tls_client->ssl);
6363
tls_client->ssl = NULL;
6464
}
65+
66+
if (tls_client->http2_ctx != NULL) {
67+
http2_ctx_put(tls_client->http2_ctx);
68+
tls_client->http2_ctx = NULL;
69+
}
6570
pthread_mutex_destroy(&tls_client->ssl_lock);
6671
} else if (conn->type == DNS_CONN_TYPE_TLS_SERVER || conn->type == DNS_CONN_TYPE_HTTPS_SERVER) {
6772
struct dns_server_conn_tls_server *tls_server = (struct dns_server_conn_tls_server *)conn;

src/dns_server/server_http2.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "connection.h"
2222
#include "dns_server.h"
2323
#include "server_tls.h"
24+
#include "smartdns/dns_conf.h"
2425
#include "smartdns/http2.h"
2526
#include "smartdns/tlog.h"
2627
#include "smartdns/util.h"
@@ -216,10 +217,6 @@ int _dns_server_process_http2(struct dns_server_conn_tls_client *tls_client, str
216217
tlog(TLOG_ERROR, "init http2 context failed.");
217218
return -1;
218219
}
219-
if (tls_client->http2_ctx != NULL) {
220-
http2_ctx_close(tls_client->http2_ctx);
221-
}
222-
tls_client->http2_ctx = ctx;
223220

224221
/* Perform initial handshake */
225222
ret = http2_ctx_handshake(ctx);
@@ -230,8 +227,11 @@ int _dns_server_process_http2(struct dns_server_conn_tls_client *tls_client, str
230227
log_level = TLOG_DEBUG; /* Less noisy for clients that disconnect early or misbehave */
231228
}
232229
tlog(log_level, "http2 handshake failed, ret=%d (%s), alpn=%s.", ret, err_msg, tls_client->alpn_selected);
230+
http2_ctx_close(ctx);
233231
return -1;
234232
}
233+
234+
tls_client->http2_ctx = ctx;
235235
}
236236

237237
/* Handle EPOLLOUT - flush pending writes */
@@ -252,7 +252,7 @@ int _dns_server_process_http2(struct dns_server_conn_tls_client *tls_client, str
252252
struct http2_poll_item poll_items[10];
253253
int poll_count = 0;
254254
int loop_count = 0;
255-
const int MAX_LOOP_COUNT = 128;
255+
const int MAX_LOOP_COUNT = 512;
256256

257257
/* Ensure handshake is complete */
258258
ret = http2_ctx_handshake(ctx);

src/http_parse/http2.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ static struct http2_stream *_http2_create_stream(struct http2_ctx *ctx, uint32_t
624624
}
625625

626626
stream->ctx = ctx;
627-
stream->refcount = 1; /* Initial reference count */
627+
stream->refcount = 0; /* Initial reference count */
628628
stream->stream_id = stream_id;
629629
stream->state = HTTP2_STREAM_IDLE;
630630

@@ -649,10 +649,12 @@ static struct http2_stream *_http2_create_stream(struct http2_ctx *ctx, uint32_t
649649
INIT_LIST_HEAD(&stream->header_list.list);
650650
hash_init(stream->header_map);
651651

652+
http2_stream_get(stream); /* Hold ownership for ctx */
653+
pthread_mutex_lock(&ctx->mutex);
652654
hash_add(ctx->stream_map, &stream->hash_node, stream->stream_id);
653655
list_add(&stream->node, &ctx->streams);
654656
ctx->active_streams++;
655-
http2_ctx_get(ctx);
657+
pthread_mutex_unlock(&ctx->mutex);
656658

657659
return stream;
658660
}
@@ -1623,7 +1625,6 @@ void http2_stream_close(struct http2_stream *stream)
16231625
pthread_mutex_unlock(&ctx->mutex);
16241626

16251627
_http2_remove_stream(stream);
1626-
http2_ctx_put(ctx);
16271628
}
16281629
/* Mark stream as closed */
16291630
stream->state = HTTP2_STREAM_CLOSED;

src/include/smartdns/http2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct http2_stream;
3232

3333
/* HTTP/2 Settings structure */
3434
struct http2_settings {
35-
int max_concurrent_streams; /* -1 = use default (4096), 0 = unlimited */
35+
int max_concurrent_streams; /* -1 = use default (8192), 0 = unlimited */
3636
};
3737

3838
/* Error codes */

0 commit comments

Comments
 (0)