summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_unit.c
diff options
context:
space:
mode:
authorMax Romanov <max.romanov@nginx.com>2020-04-10 16:21:58 +0300
committerMax Romanov <max.romanov@nginx.com>2020-04-10 16:21:58 +0300
commit58cc13ab291cac5b13462006e3feb780178ef5f3 (patch)
tree0c52174f577f308b097922a1f5c8f14f86169355 /src/nxt_unit.c
parentc7f5c1c6641838006088524c2122eae8f9c30431 (diff)
downloadunit-58cc13ab291cac5b13462006e3feb780178ef5f3.tar.gz
unit-58cc13ab291cac5b13462006e3feb780178ef5f3.tar.bz2
Resolving a racing condition while adding ports on the app's side.
An earlier attempt (ad6265786871) to resolve this condition on the router's side added a new issue: the app could get a request before acquiring a port.
Diffstat (limited to '')
-rw-r--r--src/nxt_unit.c28
1 files changed, 25 insertions, 3 deletions
diff --git a/src/nxt_unit.c b/src/nxt_unit.c
index c2e7f198..67244420 100644
--- a/src/nxt_unit.c
+++ b/src/nxt_unit.c
@@ -4282,16 +4282,38 @@ nxt_unit_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port)
int rc;
nxt_unit_impl_t *lib;
nxt_unit_process_t *process;
- nxt_unit_port_impl_t *new_port;
+ nxt_unit_port_impl_t *new_port, *old_port;
lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit);
+ pthread_mutex_lock(&lib->mutex);
+
+ old_port = nxt_unit_port_hash_find(&lib->ports, &port->id, 0);
+
+ if (nxt_slow_path(old_port != NULL)) {
+ nxt_unit_debug(ctx, "add_port: duplicate %d,%d in_fd %d out_fd %d",
+ port->id.pid, port->id.id,
+ port->in_fd, port->out_fd);
+
+ if (port->in_fd != -1) {
+ close(port->in_fd);
+ port->in_fd = -1;
+ }
+
+ if (port->out_fd != -1) {
+ close(port->out_fd);
+ port->out_fd = -1;
+ }
+
+ pthread_mutex_unlock(&lib->mutex);
+
+ return NXT_UNIT_OK;
+ }
+
nxt_unit_debug(ctx, "add_port: %d,%d in_fd %d out_fd %d",
port->id.pid, port->id.id,
port->in_fd, port->out_fd);
- pthread_mutex_lock(&lib->mutex);
-
process = nxt_unit_process_get(ctx, port->id.pid);
if (nxt_slow_path(process == NULL)) {
rc = NXT_UNIT_ERROR;