Skip to content

Commit

Permalink
Prevented scheduling events on a shared connection.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
arut committed Jan 14, 2019
1 parent b6b39b2 commit 36a0713
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/event/ngx_event_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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

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

goto next;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -453,7 +457,9 @@ ngx_udp_shared_recv(ngx_connection_t *c, u_char *buf, size_t size)
ngx_memcpy(buf, b->pos, n);

c->udp->buffer = NULL;

c->read->ready = 0;
c->read->active = 1;

return n;
}
Expand Down
4 changes: 2 additions & 2 deletions src/stream/ngx_stream_proxy_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1667,13 +1667,13 @@ ngx_stream_proxy_process(ngx_stream_session_t *s, ngx_uint_t from_upstream,

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;
}
Expand Down

0 comments on commit 36a0713

Please sign in to comment.