Welcome! Log In Create A New Profile

Advanced

[nginx] Prevented scheduling events on a shared connection.

Roman Arutyunyan
January 17, 2019 07:14AM
details: https://hg.nginx.org/nginx/rev/6d4bc025c5a7
branches:
changeset: 7440:6d4bc025c5a7
user: Roman Arutyunyan <arut@nginx.com>
date: Mon Jan 14 20:36:23 2019 +0300
description:
Prevented scheduling events on a shared connection.

A shared connection does not own its file descriptor, which means that
ngx_handle_read_event/ngx_handle_write_event calls should do nothing for it.
Currently the c->shared flag is checked in several places in the stream proxy
module prior to calling these functions. However it was not done everywhere.
Missing checks could lead to calling
ngx_handle_read_event/ngx_handle_write_event on shared connections.

The problem manifested itself when using proxy_upload_rate and resulted in
either duplicate file descriptor error (e.g. with epoll) or incorrect further
udp packet processing (e.g. with kqueue).

The fix is to set and reset the event active flag in a way that prevents
ngx_handle_read_event/ngx_handle_write_event from scheduling socket events.

diffstat:

src/event/ngx_event_udp.c | 6 ++++++
src/stream/ngx_stream_proxy_module.c | 4 ++--
2 files changed, 8 insertions(+), 2 deletions(-)

diffs (58 lines):

diff -r 5efc23d83bc2 -r 6d4bc025c5a7 src/event/ngx_event_udp.c
--- a/src/event/ngx_event_udp.c Thu Jan 17 14:31:04 2019 +0300
+++ b/src/event/ngx_event_udp.c Mon Jan 14 20:36:23 2019 +0300
@@ -256,7 +256,9 @@ ngx_event_recvmsg(ngx_event_t *ev)
rev = c->read;

c->udp->buffer = &buf;
+
rev->ready = 1;
+ rev->active = 0;

rev->handler(rev);

@@ -265,6 +267,7 @@ ngx_event_recvmsg(ngx_event_t *ev)
}

rev->ready = 0;
+ rev->active = 1;

goto next;
}
@@ -343,6 +346,7 @@ ngx_event_recvmsg(ngx_event_t *ev)
rev = c->read;
wev = c->write;

+ rev->active = 1;
wev->ready = 1;

rev->log = log;
@@ -453,7 +457,9 @@ ngx_udp_shared_recv(ngx_connection_t *c,
ngx_memcpy(buf, b->pos, n);

c->udp->buffer = NULL;
+
c->read->ready = 0;
+ c->read->active = 1;

return n;
}
diff -r 5efc23d83bc2 -r 6d4bc025c5a7 src/stream/ngx_stream_proxy_module.c
--- a/src/stream/ngx_stream_proxy_module.c Thu Jan 17 14:31:04 2019 +0300
+++ b/src/stream/ngx_stream_proxy_module.c Mon Jan 14 20:36:23 2019 +0300
@@ -1667,13 +1667,13 @@ ngx_stream_proxy_process(ngx_stream_sess

flags = src->read->eof ? NGX_CLOSE_EVENT : 0;

- if (!src->shared && ngx_handle_read_event(src->read, flags) != NGX_OK) {
+ if (ngx_handle_read_event(src->read, flags) != NGX_OK) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}

if (dst) {
- if (!dst->shared && ngx_handle_write_event(dst->write, 0) != NGX_OK) {
+ if (ngx_handle_write_event(dst->write, 0) != NGX_OK) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[nginx] Prevented scheduling events on a shared connection.

Roman Arutyunyan 257 January 17, 2019 07:14AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 325
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready