{"action":"opened","issue":{"url":"https://api.github.com/repos/h2o/h2o/issues/1256","repository_url":"https://api.github.com/repos/h2o/h2o","labels_url":"https://api.github.com/repos/h2o/h2o/issues/1256/labels{/name}","comments_url":"https://api.github.com/repos/h2o/h2o/issues/1256/comments","events_url":"https://api.github.com/repos/h2o/h2o/issues/1256/events","html_url":"https://github.com/h2o/h2o/issues/1256","id":221373101,"number":1256,"title":"leaks in examples/libh2o/simple.c","user":{"login":"dmitri--","id":1101301,"avatar_url":"https://avatars1.githubusercontent.com/u/1101301?v=3","gravatar_id":"","url":"https://api.github.com/users/dmitri--","html_url":"https://github.com/dmitri--","followers_url":"https://api.github.com/users/dmitri--/followers","following_url":"https://api.github.com/users/dmitri--/following{/other_user}","gists_url":"https://api.github.com/users/dmitri--/gists{/gist_id}","starred_url":"https://api.github.com/users/dmitri--/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dmitri--/subscriptions","organizations_url":"https://api.github.com/users/dmitri--/orgs","repos_url":"https://api.github.com/users/dmitri--/repos","events_url":"https://api.github.com/users/dmitri--/events{/privacy}","received_events_url":"https://api.github.com/users/dmitri--/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-04-12T19:56:54Z","updated_at":"2017-04-12T19:56:54Z","closed_at":null,"body":"Seems, this example is very simple so all the resource housekeeping left over. If it's the purpose, that's okay. On the other hand, I think it makes sense to make it leak-free so it would be easier to check if H2O itself has leaks. Changes are not complicated. Feel free to merge:\r\n\r\n```\r\ndiff --git a/examples/libh2o/simple.c b/examples/libh2o/simple.c\r\nindex 282da51..dec6efb 100644\r\n--- a/examples/libh2o/simple.c\r\n+++ b/examples/libh2o/simple.c\r\n@@ -93,6 +93,9 @@ static h2o_globalconf_t config;\r\n static h2o_context_t ctx;\r\n static h2o_multithread_receiver_t libmemcached_receiver;\r\n static h2o_accept_ctx_t accept_ctx;\r\n+#if H2O_USE_LIBUV\r\n+static uv_tcp_t listener;\r\n+#endif\r\n \r\n #if H2O_USE_LIBUV\r\n \r\n@@ -118,7 +121,6 @@ static void on_accept(uv_stream_t *listener, int status)\r\n \r\n static int create_listener(void)\r\n {\r\n-    static uv_tcp_t listener;\r\n     struct sockaddr_in addr;\r\n     int r;\r\n \r\n@@ -215,6 +217,12 @@ static int setup_ssl(const char *cert_file, const char *key_file)\r\n     return 0;\r\n }\r\n \r\n+#if H2O_USE_LIBUV\r\n+static void on_sigint(uv_signal_t *req, int signum) {\r\n+    uv_stop(ctx.loop);\r\n+}\r\n+#endif\r\n+\r\n int main(int argc, char **argv)\r\n {\r\n     h2o_hostconf_t *hostconf;\r\n@@ -230,6 +238,7 @@ int main(int argc, char **argv)\r\n \r\n #if H2O_USE_LIBUV\r\n     uv_loop_t loop;\r\n+    uv_signal_t sigint;\r\n     uv_loop_init(&loop);\r\n     h2o_context_init(&ctx, &loop, &config);\r\n #else\r\n@@ -253,7 +262,18 @@ int main(int argc, char **argv)\r\n     }\r\n \r\n #if H2O_USE_LIBUV\r\n+    uv_signal_init(ctx.loop, &sigint);\r\n+    uv_signal_start(&sigint, on_sigint, SIGINT);\r\n+\r\n+    uv_run(ctx.loop, UV_RUN_DEFAULT); /* main loop */\r\n+\r\n+    uv_signal_stop(&sigint);\r\n+    uv_close((uv_handle_t *)&sigint, NULL);\r\n+    uv_close((uv_handle_t *)&listener, NULL);\r\n+    h2o_context_dispose(&ctx);\r\n+    h2o_config_dispose(&config);\r\n     uv_run(ctx.loop, UV_RUN_DEFAULT);\r\n+    uv_loop_close(ctx.loop);\r\n #else\r\n     while (h2o_evloop_run(ctx.loop, INT32_MAX) == 0)\r\n         ;\r\n\r\n```\r\nThese changes add SIGINT handler so it's very simple to break the loop by pressing Ctrl-C in console or by sending this signal using `kill`. Also they add all these *_stop, *_close, *_dispose calls.\r\nI compile the sample with gcc's linker option: \"-fsanitize=leak\" and check the output after the server shutdown. This way I discovered #1255.\r\nHope it's useful for somebody else too.\r\nPerhaps it makes sense to add something like that into standard tests to make sure no leaks are introduced."}}
{"actor":{"display_login":"dmitri--"}}