diff --git a/lib/core/context.c b/lib/core/context.c index 10d2762a2..507387ba7 100644 --- a/lib/core/context.c +++ b/lib/core/context.c @@ -1082,7 +1082,7 @@ lws_context_destroy(struct lws_context *context) if (!context->being_destroyed2) { lws_context_destroy2(context); - goto out; + return; } lwsl_info("%s: ctx %p: already being destroyed\n", __func__, context); @@ -1180,6 +1180,8 @@ lws_context_destroy(struct lws_context *context) return; +#if defined(LWS_WITH_NETWORK) out: context->inside_context_destroy = 0; +#endif } diff --git a/lib/plat/unix/unix-init.c b/lib/plat/unix/unix-init.c index 24490bbc0..72c599597 100644 --- a/lib/plat/unix/unix-init.c +++ b/lib/plat/unix/unix-init.c @@ -35,10 +35,6 @@ #endif #include -#if defined(LWS_HAVE_MALLOC_TRIM) -#include -#endif - #if defined(LWS_WITH_NETWORK) static void lws_sul_plat_unix(lws_sorted_usec_list_t *sul) @@ -53,9 +49,6 @@ lws_sul_plat_unix(lws_sorted_usec_list_t *sul) kill(pt->context->started_with_parent, 0) < 0) kill(getpid(), SIGTERM); #endif -#if defined(LWS_HAVE_MALLOC_TRIM) - malloc_trim(4 * 1024); -#endif if (pt->context->deprecated && !pt->context->count_wsi_allocated) { lwsl_notice("%s: ending deprecated context\n", __func__); diff --git a/lwsws/main.c b/lwsws/main.c index 64d7c2da3..63a81983d 100644 --- a/lwsws/main.c +++ b/lwsws/main.c @@ -1,7 +1,7 @@ /* * libwebsockets web server application * - * Written in 2010-2019 by Andy Green + * Written in 2010-2020 by Andy Green * * This file is made available under the Creative Commons CC0 1.0 * Universal Public Domain Dedication. @@ -51,7 +51,12 @@ int fork(void) #include +#if defined(LWS_HAVE_MALLOC_TRIM) +#include +#endif + static struct lws_context *context; +static lws_sorted_usec_list_t sul_lwsws; static char config_dir[128]; static int opts = 0, do_reload = 1; static uv_loop_t loop; @@ -111,6 +116,18 @@ void signal_cb(uv_signal_t *watcher, int signum) lws_context_destroy(context); } +static void +lwsws_min(lws_sorted_usec_list_t *sul) +{ + lwsl_debug("%s\n", __func__); + +#if defined(LWS_HAVE_MALLOC_TRIM) + malloc_trim(4 * 1024); +#endif + + lws_sul_schedule(context, 0, &sul_lwsws, lwsws_min, 60 * LWS_US_PER_SEC); +} + static int context_creation(void) { @@ -162,6 +179,8 @@ context_creation(void) if (lwsws_get_config_vhosts(context, &info, config_dir, &cs, &cs_len)) return 1; + lws_sul_schedule(context, 0, &sul_lwsws, lwsws_min, 60 * LWS_US_PER_SEC); + return 0; init_failed: @@ -287,8 +306,8 @@ int main(int argc, char **argv) lws_set_log_level(debug_level, lwsl_emit_stderr_notimestamp); - lwsl_notice("lwsws libwebsockets web server - license CC0 + LGPL2.1\n"); - lwsl_notice("(C) Copyright 2010-2018 Andy Green \n"); + lwsl_notice("lwsws libwebsockets web server - license CC0 + MIT\n"); + lwsl_notice("(C) Copyright 2010-2020 Andy Green \n"); #if (UV_VERSION_MAJOR > 0) // Travis... uv_loop_init(&loop); @@ -315,6 +334,9 @@ int main(int argc, char **argv) uv_close((uv_handle_t *)&signal_outer[n], NULL); } + /* cancel the per-minute sul */ + lws_sul_schedule(context, 0, &sul_lwsws, NULL, LWS_SET_TIMER_USEC_CANCEL); + lws_context_destroy(context); (void)budget; #if (UV_VERSION_MAJOR > 0) // Travis...