Skip to content

Commit 8653320

Browse files
committed
main: close active connections on shutdown
If there are any active API connections when shutting down the server with SIGINT or SIGTERM, there will be a memory leak reported by valgrind and ASAN. Make sure to free and close their related FDs. Signed-off-by: Robin Jarry <[email protected]>
1 parent 91b6872 commit 8653320

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

main/main.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,13 @@ static int listen_api_socket(void) {
338338
return 0;
339339
}
340340

341+
static int ev_close(const struct event_base *, const struct event *ev, void *) {
342+
event_callback_fn cb = event_get_callback(ev);
343+
if (cb == api_read_cb || cb == api_write_cb)
344+
event_free_finalize(0, (struct event *)ev, finalize_close_fd);
345+
return 0;
346+
}
347+
341348
int main(int argc, char **argv) {
342349
int ret = EXIT_FAILURE;
343350

@@ -373,8 +380,10 @@ int main(int argc, char **argv) {
373380
unregister_signals();
374381
if (ev_listen)
375382
event_free_finalize(0, ev_listen, finalize_close_fd);
376-
if (ev_base)
383+
if (ev_base) {
384+
event_base_foreach_event(ev_base, ev_close, NULL);
377385
event_base_free(ev_base);
386+
}
378387
unlink(args.api_sock_path);
379388
libevent_global_shutdown();
380389
modules_fini();

0 commit comments

Comments
 (0)