diff --git a/lib/api.c b/lib/api.c index b11e1db01..a8bdc6504 100644 --- a/lib/api.c +++ b/lib/api.c @@ -56,6 +56,8 @@ int api_ws_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void * if (ret) return -1; + list_push(&w->api->sessions, s); + lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), s->peer.name, sizeof(s->peer.name), s->peer.ip, sizeof(s->peer.ip)); debug(LOG_API, "New API session initiated: version=%d, mode=websocket, remote=%s (%s)", s->version, s->peer.name, s->peer.ip); @@ -65,6 +67,8 @@ int api_ws_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void * ret = api_session_destroy(s); if (ret) return -1; + + list_remove(&w->api->sessions, s); debug(LOG_API, "Closed API session"); @@ -120,6 +124,8 @@ int api_http_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void if (ret) return -1; + list_push(&w->api->sessions, s); + lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), s->peer.name, sizeof(s->peer.name), s->peer.ip, sizeof(s->peer.ip)); debug(LOG_API, "New API session initiated: version=%d, mode=http, remote=%s (%s)", s->version, s->peer.name, s->peer.ip); @@ -147,6 +153,9 @@ int api_http_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void ret = api_session_destroy(s); if (ret) return -1; + + list_remove(&w->api->sessions, s); + break; case LWS_CALLBACK_HTTP_BODY: @@ -170,7 +179,7 @@ int api_http_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void web_buffer_write(&s->response.body, wsi); if (s->completed && s->response.body.len == 0) - return -1; + return -1; /* Close connection */ break; default: @@ -213,6 +222,11 @@ int api_start(struct api *a) int api_stop(struct api *a) { info("Stopping API sub-system"); + + for (int i = 0; i < 10 && list_length(&a->sessions) > 0; i++) { + info("Wait for API requests to complete"); + usleep(100 * 1e-3); + } list_destroy(&a->sessions, (dtor_cb_t) api_session_destroy, false); diff --git a/lib/super_node.c b/lib/super_node.c index f6dd10888..0541fb023 100644 --- a/lib/super_node.c +++ b/lib/super_node.c @@ -431,11 +431,11 @@ int super_node_stop(struct super_node *sn) } } -#ifdef WITH_WEB - web_stop(&sn->web); -#endif #ifdef WITH_API api_stop(&sn->api); +#endif +#ifdef WITH_WEB + web_stop(&sn->web); #endif log_stop(&sn->log); diff --git a/lib/web.c b/lib/web.c index 5e0f7a3ee..a6e8a7a8d 100644 --- a/lib/web.c +++ b/lib/web.c @@ -236,7 +236,7 @@ int web_start(struct web *w) int web_stop(struct web *w) { - if (w->state == STATE_STARTED) + if (w->state != STATE_STARTED) return 0; info("Stopping Web sub-system");