Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A crash bugfix for ngx_http_dyups_module #1133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cuber
Copy link

@cuber cuber commented Nov 30, 2018

Upstream request will crash if there is a first shown prefix variable in dynamic update.

server {
  listen 8000;

  location / {
    dyups_interface;
  }
}

server {
  listen 8080;
  location / {
    proxy_pass http://$host;
  }
}
$curl "127.0.0.1:8000/upstream/dyhost" -d 'consistent_hash $arg_ds; server 127.0.0.1:8081; server 127.0.0.1:8082;'
success
$curl "127.0.0.1:8080/foobar" -H'host: dyhost'
#crash happened

@CLAassistant
Copy link

CLAassistant commented Nov 30, 2018

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@FengXingYuXin
Copy link
Contributor

我理解 修复代码中,楼主为了复用函数ngx_http_variables_init_vars,做了很多准备工作,只是为新变量结构体设置get_handler、data、flag,是否可以直接遍历cmcf->variables,针对get_handler为空的情况 按照prefix_variables类别,设置对应的get_handler、data、flags就行了?

cmcf->variables_keys = &vk;

v = va_prev.elts;
for (i = 0; i < va_prev.nelts; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是不是可以逆序遍历,遇到get_hander不为NULL,直接退出?减少遍历的时间

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得修改成逆序遍历提前退出带来的提升并不会太大, 理由有如下两个

  1. 相比于 dyups 的消耗, 遍历 cmcf->variables 的成本并不高
  2. cmcf->variables 的遍历是顺序访存

Copy link
Author

@cuber cuber Jan 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FengXingYuXin 已修改为逆序遍历 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cuber 还有一个疑问,如果请求ngx_http_request_t中已经分配了variables(存储变量值),正处于接收请求body的阶段(可能会有多次网络IO),如果dyups有更新(consistent_hash+新自定义变量),那在请求接收完body被转发时,触发渲染新变量的逻辑,但新变量并没有在variables中分配对应内存,所以ngx_http_get_indexed_variable时,还是会导致r->variables[index]的index越界吧?

Copy link
Author

@cuber cuber Jan 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FengXingYuXin
的确有越界风险,而且是从 ngx_http_script_run 入口开始就会有风险了
考虑如下情况:

  1. 请求 A 创建时 r->variables 是按照 cmcf->variables 个数来 alloc
  2. A 正在异步等待 upstream body
  3. 来了个 dyups 的请求 B,把 cmcf->variables 给 append 了一个 element
  4. 前面异步等待的 body 的请求 A 回来了以后,在 ngx_http_script_run 阶段就会越界
    cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);

    for (i = 0; i < cmcf->variables.nelts; i++) {
        if (r->variables[i].no_cacheable) {
            r->variables[i].valid = 0;
            r->variables[i].not_found = 0;
        }
    }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FengXingYuXin
改了下,现在应该 ok 了

@cuber cuber force-pushed the bugfix/ngx_http_dyups_module branch 3 times, most recently from 69e1f10 to c08394a Compare January 24, 2019 10:21
@cuber cuber force-pushed the bugfix/ngx_http_dyups_module branch from c08394a to f108aec Compare January 30, 2019 03:38
@yunoasgit
Copy link

@ https://github.com/yzprofile/ngx_http_dyups_module
Has this version been fixed?

@wangfakang
Copy link
Collaborator

@cuber could you resolve the conflict ? THX.

Upstream request will crash if there is a first shown prefix variable in dynamic.

server {
  listen 8000;

  location / {
    dyups_interface;
  }
}

server {
  listen 8080;
  location / {
    proxy_pass http://$host;
  }
}

$curl "127.0.0.1:8000/upstream/dyhost" -d 'consistent_hash $arg_ds; server 127.0.0.1:8081; server 127.0.0.1:8082;'
success
$curl "127.0.0.1:8080/foobar" -H'host: dyhost'
crash happened
@cuber cuber force-pushed the bugfix/ngx_http_dyups_module branch from f108aec to 61710a1 Compare April 10, 2019 10:01
@cuber
Copy link
Author

cuber commented Apr 10, 2019

@wangfakang I have adapted this patch to the new version of Tengine, please take a look :-)

@chobits
Copy link
Member

chobits commented May 6, 2019

Hi all,

this issue has been fixed in dyups module, see yzprofile/ngx_http_dyups_module#104

@chobits
Copy link
Member

chobits commented May 6, 2019

Hi @cuber

I will close this issue later, because we have one pullrequest to update yzprofile/dyups module: #1255.

But I note that this pull request has one difference with yzprofile/dyups bugfix pr.

typedef struct {
    ngx_uint_t                           ref;
+    ngx_http_dyups_srv_conf_t           *duscf;       <<< here!!!
    ngx_http_upstream_init_peer_pt       init;
} ngx_http_dyups_upstream_srv_conf_t;

Can yzprofile/dyups bugfix pr replace this pull request?


We hope that tengine/dyups can keep pace with yzprofile/dyups.

@chobits chobits self-requested a review May 6, 2019 12:37
@cuber
Copy link
Author

cuber commented May 7, 2019

Hi @chobits,
Actually, #1133 dose more than yzprofile/dyups#104
I will make another pr to yzprofile/dyups, so tengine can keep pace with the origin repository

@chobits
Copy link
Member

chobits commented May 8, 2019

hi all

Because #1255 (updated to yzprofile/dyups) has been merged.

This pr has conflicts again.

@cuber, you should open a pr to yzprofile/dyups first, our dev team will review your pr in yzprofile/dyups and then merge it to tengine/dyups.

@cuber
Copy link
Author

cuber commented May 14, 2019

Hi @chobits @wangfakang
I have created a new PR for yzprofile/ngx_http_dyups_module#104
In the mean time, i have found a dangling pointer bug for modules/ngx_http_upstream_check_module.
To fix this bug, i have create a new PR to tengine #1265

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants