Node.js: added correct exit processing.

Node.js processes didn't exit after the changes in b9f7635e6be2,
as the quit command from port wasn't handled by the module.
This commit is contained in:
Alexander Borisov
2018-11-15 15:39:34 +03:00
parent 71f4cb9134
commit 06b404feb1
2 changed files with 43 additions and 18 deletions

View File

@@ -14,6 +14,12 @@
napi_ref Unit::constructor_; napi_ref Unit::constructor_;
struct nxt_nodejs_ctx_t {
nxt_unit_port_id_t port_id;
uv_poll_t poll;
};
Unit::Unit(napi_env env): Unit::Unit(napi_env env):
env_(env), env_(env),
wrapper_(nullptr), wrapper_(nullptr),
@@ -197,6 +203,7 @@ Unit::create_server(napi_env env, napi_callback_info info)
unit_init.callbacks.request_handler = request_handler; unit_init.callbacks.request_handler = request_handler;
unit_init.callbacks.add_port = add_port; unit_init.callbacks.add_port = add_port;
unit_init.callbacks.remove_port = remove_port; unit_init.callbacks.remove_port = remove_port;
unit_init.callbacks.quit = quit;
obj->unit_ctx_ = nxt_unit_init(&unit_init); obj->unit_ctx_ = nxt_unit_init(&unit_init);
if (obj->unit_ctx_ == NULL) { if (obj->unit_ctx_ == NULL) {
@@ -355,11 +362,11 @@ nxt_uv_read_callback(uv_poll_t *handle, int status, int events)
int int
Unit::add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) Unit::add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port)
{ {
int err; int err;
Unit *obj; Unit *obj;
uv_loop_t *loop; uv_loop_t *loop;
uv_poll_t *uv_handle; napi_status status;
napi_status status; nxt_nodejs_ctx_t *node_ctx;
if (port->in_fd != -1) { if (port->in_fd != -1) {
obj = reinterpret_cast<Unit *>(ctx->unit->data); obj = reinterpret_cast<Unit *>(ctx->unit->data);
@@ -376,48 +383,65 @@ Unit::add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port)
return NXT_UNIT_ERROR; return NXT_UNIT_ERROR;
} }
uv_handle = new uv_poll_t; node_ctx = new nxt_nodejs_ctx_t;
err = uv_poll_init(loop, uv_handle, port->in_fd); err = uv_poll_init(loop, &node_ctx->poll, port->in_fd);
if (err < 0) { if (err < 0) {
napi_throw_error(obj->env_, NULL, "Failed to init uv.poll"); napi_throw_error(obj->env_, NULL, "Failed to init uv.poll");
return NXT_UNIT_ERROR; return NXT_UNIT_ERROR;
} }
err = uv_poll_start(uv_handle, UV_READABLE, nxt_uv_read_callback); err = uv_poll_start(&node_ctx->poll, UV_READABLE, nxt_uv_read_callback);
if (err < 0) { if (err < 0) {
napi_throw_error(obj->env_, NULL, "Failed to start uv.poll"); napi_throw_error(obj->env_, NULL, "Failed to start uv.poll");
return NXT_UNIT_ERROR; return NXT_UNIT_ERROR;
} }
port->data = uv_handle; ctx->data = node_ctx;
uv_handle->data = ctx;
node_ctx->port_id = port->id;
node_ctx->poll.data = ctx;
} }
return nxt_unit_add_port(ctx, port); return nxt_unit_add_port(ctx, port);
} }
inline bool
operator == (const nxt_unit_port_id_t &p1, const nxt_unit_port_id_t &p2)
{
return p1.pid == p2.pid && p1.id == p2.id;
}
void void
Unit::remove_port(nxt_unit_ctx_t *ctx, nxt_unit_port_id_t *port_id) Unit::remove_port(nxt_unit_ctx_t *ctx, nxt_unit_port_id_t *port_id)
{ {
nxt_unit_port_t *port; nxt_nodejs_ctx_t *node_ctx;
port = nxt_unit_find_port(ctx, port_id); if (ctx->data != NULL) {
if (port == NULL) { node_ctx = (nxt_nodejs_ctx_t *) ctx->data;
return;
}
if (port->in_fd != -1 && port->data != NULL) { if (node_ctx->port_id == *port_id) {
uv_poll_stop((uv_poll_t *) port->data); uv_poll_stop(&node_ctx->poll);
delete (uv_poll_t *) port->data; delete node_ctx;
ctx->data = NULL;
}
} }
nxt_unit_remove_port(ctx, port_id); nxt_unit_remove_port(ctx, port_id);
} }
void
Unit::quit(nxt_unit_ctx_t *ctx)
{
nxt_unit_done(ctx);
}
napi_value napi_value
Unit::get_server_object() Unit::get_server_object()
{ {

View File

@@ -40,6 +40,7 @@ private:
static void request_handler(nxt_unit_request_info_t *req); static void request_handler(nxt_unit_request_info_t *req);
static int add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); static int add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port);
static void remove_port(nxt_unit_ctx_t *ctx, nxt_unit_port_id_t *port_id); static void remove_port(nxt_unit_ctx_t *ctx, nxt_unit_port_id_t *port_id);
static void quit(nxt_unit_ctx_t *ctx);
napi_value get_server_object(); napi_value get_server_object();