kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 790d020ce9a0289850f52bd092e31346ffda263e
parent 0430c36a0869fe7f47c1f156ca769512d2fb4a7d
Author: Joris Vink <joris@coders.se>
Date:   Tue, 22 Oct 2019 17:06:32 +0200

Stop a python coro from getting stuck with httpclient.

In cases where a request is immediately completed in libcurl its multi
handle and no additional i/o is happening a coro can get stuck waiting
to be run.

Prevent this by lowering netwait from KORE_WAIT_INFINITE if there
are pending python coroutines.

Diffstat:
include/kore/python_api.h | 1+
src/kore.c | 2+-
src/python.c | 14++++++++++++++
src/worker.c | 13++++++++-----
4 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/include/kore/python_api.h b/include/kore/python_api.h @@ -29,6 +29,7 @@ void kore_python_preinit(void); void kore_python_cleanup(void); void kore_python_coro_run(void); void kore_python_proc_reap(void); +int kore_python_coro_pending(void); void kore_python_path(const char *); void kore_python_coro_delete(void *); void kore_python_log_error(const char *); diff --git a/src/kore.c b/src/kore.c @@ -129,7 +129,7 @@ version(void) printf("no-http "); #endif #if defined(KORE_USE_CURL) - printf("curl "); + printf("curl-%s ", LIBCURL_VERSION); #endif #if defined(KORE_USE_PGSQL) printf("pgsql "); diff --git a/src/python.c b/src/python.c @@ -376,6 +376,14 @@ kore_python_coro_run(void) * to HTTP requests was awoken and only run if true? */ http_process(); + +#if defined(KORE_USE_CURL) + /* + * If a coroutine fired off an httpclient instance, immediately + * let it make progress. + */ + kore_curl_do_timeout(); +#endif } void @@ -403,6 +411,12 @@ kore_python_coro_delete(void *obj) kore_pool_put(&coro_pool, coro); } +int +kore_python_coro_pending(void) +{ + return (!TAILQ_EMPTY(&coro_runnable)); +} + void kore_python_log_error(const char *function) { diff --git a/src/worker.c b/src/worker.c @@ -416,10 +416,16 @@ kore_worker_entry(struct kore_worker *kw) netwait = kore_timer_next_run(now); + if (netwait == KORE_WAIT_INFINITE) { #if !defined(KORE_NO_HTTP) - if (netwait == KORE_WAIT_INFINITE && http_request_count > 0) - netwait = 100; + if (http_request_count > 0) + netwait = 100; #endif +#if defined(KORE_USE_PYTHON) + if (kore_python_coro_pending()) + netwait = 10; +#endif + } kore_platform_event_wait(netwait); now = kore_time_ms(); @@ -469,9 +475,6 @@ kore_worker_entry(struct kore_worker *kw) #if defined(KORE_USE_PYTHON) kore_python_coro_run(); #endif -#if defined(KORE_USE_CURL) - kore_curl_do_timeout(); -#endif if (next_prune <= now) { kore_connection_check_timeout(now); kore_connection_prune(KORE_CONNECTION_PRUNE_DISCONNECT);