From 61ea83cbc019db5c000baf4debf873be59528e63 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 4 Oct 2021 18:08:34 +0200 Subject: report reducer error instead of crashing --- src/reducer/anastasis_api_redux.c | 61 +++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/src/reducer/anastasis_api_redux.c b/src/reducer/anastasis_api_redux.c index 796e60d..92801f4 100644 --- a/src/reducer/anastasis_api_redux.c +++ b/src/reducer/anastasis_api_redux.c @@ -1490,6 +1490,31 @@ dummy_cleanup (void *cls) } +/** + * Closure for external_redux_done. + */ +struct ExternalReduxCls +{ + ANASTASIS_ActionCallback cb; + void *cb_cls; + json_t *new_state; +}; + + +/** + * Callback called when the redux action has been processed by + * the external reducer. + */ +static void +external_redux_done (void *cls) +{ + struct ExternalReduxCls *erc = cls; + erc->cb (erc->cb_cls, + TALER_EC_NONE, + erc->new_state); +} + + /** * Handle an action using an external reducer, i.e. * by shelling out to another process. @@ -1563,9 +1588,26 @@ redux_action_external (const char *ext_reducer, GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Wrote old state to reducer stdin.\n"); - next_state = json_loadf (reducer_stdout, - 0, - NULL); + { + json_error_t err; + + next_state = json_loadf (reducer_stdout, + 0, + &err); + + if (NULL == next_state) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "External reducer did not output valid JSON: %s:%d:%d %s\n", + err.source, + err.line, + err.column, + err.text); + GNUNET_assert (0 == fclose (reducer_stdout)); + waitpid (pid, &status, 0); + return NULL; + } + } /* FIXME: report error instead! */ GNUNET_assert (NULL != next_state); @@ -1584,9 +1626,16 @@ redux_action_external (const char *ext_reducer, /* Callback is called immediately, cleanup must never be called */ act->cleanup = &dummy_cleanup; - cb (cb_cls, - TALER_EC_NONE, - next_state); + { + struct ExternalReduxCls *sched_cls = GNUNET_new (struct ExternalReduxCls); + + sched_cls->cb = cb; + sched_cls->cb_cls = cb_cls; + sched_cls->new_state = next_state; + GNUNET_SCHEDULER_add_now (external_redux_done, + sched_cls); + } + return act; } -- cgit v1.2.3