Skip to content

Commit ec6df32

Browse files
bugfix: windows also need to support ngx_http_lua_ffi_worker_pids.
1 parent 9bee7e7 commit ec6df32

File tree

4 files changed

+111
-3
lines changed

4 files changed

+111
-3
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9034,7 +9034,7 @@ ngx.worker.pids
90349034

90359035
**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, exit_worker_by_lua**
90369036

9037-
This function returns a Lua table for all Nginx worker process ID (PID). Nginx uses channel to send the current worker PID to another worker in the worker process start or restart. So this API can get all current worker PID.
9037+
This function returns a Lua table for all Nginx worker process IDs (PIDs). Nginx uses channel to send the current worker PID to another worker in the worker process start or restart. So this API can get all current worker PIDs. Windows does not have this API.
90389038

90399039
This API was first introduced in the `0.10.23` release.
90409040

src/ngx_http_lua_worker.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ ngx_http_lua_ffi_worker_pids(int *pids, size_t *pids_len)
3737
break;
3838
}
3939

40-
if (i == ngx_process_slot && ngx_processes[i].pid == 0) {
40+
/* The current process */
41+
if (i == ngx_process_slot) {
4142
pids[n++] = ngx_pid;
4243
}
4344

44-
if (ngx_processes[i].pid > 0) {
45+
if (ngx_processes[i].channel[0] > 0 && ngx_processes[i].pid > 0) {
4546
pids[n++] = ngx_processes[i].pid;
4647
}
4748
}

t/122-worker-2.t

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# vim:set ft= ts=4 sw=4 et fdm=marker:
2+
3+
use Test::Nginx::Socket::Lua;
4+
5+
#worker_connections(1014);
6+
master_on();
7+
workers(4);
8+
#log_level('warn');
9+
10+
repeat_each(2);
11+
12+
plan tests => repeat_each() * (blocks() * 3);
13+
14+
#no_diff();
15+
no_long_string();
16+
run_tests();
17+
18+
__DATA__
19+
20+
=== TEST 1: get worker pids with multiple worker
21+
--- config
22+
location /lua {
23+
content_by_lua_block {
24+
local pids, err = ngx.worker.pids()
25+
if err ~= nil then
26+
return
27+
end
28+
local pid = ngx.worker.pid()
29+
ngx.say("worker pid: ", pid)
30+
local count = ngx.worker.count()
31+
ngx.say("worker count: ", count)
32+
ngx.say("worker pids count: ", #pids)
33+
for i = 1, count do
34+
if pids[i] == pid then
35+
ngx.say("worker pid is correct.")
36+
return
37+
end
38+
end
39+
}
40+
}
41+
--- request
42+
GET /lua
43+
--- response_body_like
44+
worker pid: \d+
45+
worker count: 4
46+
worker pids count: 4
47+
worker pid is correct\.
48+
--- no_error_log
49+
[error]

t/122-worker-3.t

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# vim:set ft= ts=4 sw=4 et fdm=marker:
2+
our $SkipReason;
3+
4+
BEGIN {
5+
if ($ENV{TEST_NGINX_CHECK_LEAK}) {
6+
$SkipReason = "unavailable for the hup tests";
7+
8+
} else {
9+
$ENV{TEST_NGINX_USE_HUP} = 1;
10+
undef $ENV{TEST_NGINX_USE_STAP};
11+
}
12+
}
13+
14+
use Test::Nginx::Socket::Lua 'no_plan';
15+
16+
#worker_connections(1014);
17+
master_on();
18+
workers(4);
19+
#log_level('warn');
20+
21+
repeat_each(2);
22+
23+
#no_diff();
24+
no_long_string();
25+
run_tests();
26+
27+
__DATA__
28+
29+
=== TEST 1: get worker pids with multiple worker
30+
--- config
31+
location /lua {
32+
content_by_lua_block {
33+
local pids, err = ngx.worker.pids()
34+
if err ~= nil then
35+
return
36+
end
37+
local pid = ngx.worker.pid()
38+
ngx.say("worker pid: ", pid)
39+
local count = ngx.worker.count()
40+
ngx.say("worker count: ", count)
41+
ngx.say("worker pids count: ", #pids)
42+
for i = 1, count do
43+
if pids[i] == pid then
44+
ngx.say("worker pid is correct.")
45+
return
46+
end
47+
end
48+
}
49+
}
50+
--- request
51+
GET /lua
52+
--- response_body_like
53+
worker pid: \d+
54+
worker count: 4
55+
worker pids count: 4
56+
worker pid is correct\.
57+
--- no_error_log
58+
[error]

0 commit comments

Comments
 (0)