kingluo commented on code in PR #9266:
URL: https://github.com/apache/apisix/pull/9266#discussion_r1163934524


##########
t/plugin/grpc-transcode2.t:
##########
@@ -486,7 +486,7 @@ GET /grpc_plus?a=1&b=2
 --- response_body eval
 qr/\{"result":3\}/
 --- error_log eval
-qr/request log: 
\{.*body":\"\\u0000\\u0000\\u0000\\u0000\\u0002\\b\\u0003\\u0000\\u0000\\u0000\\u0000\\u0002\\b\\u0003"/
+qr/request log: \{.*body":\"\\u0000\\u0000\\u0000\\u0000\\u0002\\b\\u0003"/

Review Comment:
   This PR fixes another bug: insert the same chunk twice (by different 
plugins) into the `body_buffer`.
   
   Bug reproduce:
   
   ```diff
   diff --git a/apisix/core/response.lua b/apisix/core/response.lua
   index b934d94b..971961b3 100644
   --- a/apisix/core/response.lua
   +++ b/apisix/core/response.lua
   @@ -178,6 +178,8 @@ function _M.hold_body_chunk(ctx, hold_the_copy)
        local body_buffer
        local chunk, eof = arg[1], arg[2]
        if type(chunk) == "string" and chunk ~= "" then
   +        ngx.log(ngx.WARN, "#chunk: ", #chunk)
   +        ngx.log(ngx.WARN, debug.traceback("hold_body_chunk", 3))
            body_buffer = ctx._body_buffer
            if not body_buffer then
                body_buffer = {
   @@ -185,10 +187,13 @@ function _M.hold_body_chunk(ctx, hold_the_copy)
                    n = 1
                }
                ctx._body_buffer = body_buffer
   +            ngx.log(ngx.WARN, "create body_buffer: ", tostring(body_buffer))
   +            ngx.log(ngx.WARN, "init #chunk: ", #chunk)
            else
                local n = body_buffer.n + 1
                body_buffer.n = n
                body_buffer[n] = chunk
   +            ngx.log(ngx.WARN, "add #chunk: ", #chunk)
            end
        end
   ```
   
   error.log from this test case:
   
   ```
   2023/04/12 18:08:06 [warn] 2785045#2785045: *3 [lua] response.lua:181: 
hold_body_chunk(): #chunk: 7 while sending to client, client: 127.0.0.1, 
server: localhost, request: "GET /grpc_plus?a=1&b=2 HTTP/1.1", upstream: 
"grpc://127.0.0.1:50051", host: "localhost"
   2023/04/12 18:08:06 [warn] 2785045#2785045: *3 [lua] response.lua:182: 
hold_body_chunk(): hold_body_chunk
   stack traceback:
           /opt/apisix/apisix/plugins/http-logger.lua:153: in function 
'phase_func'
           /opt/apisix/apisix/plugin.lua:1131: in function 'run_plugin'
           /opt/apisix/apisix/plugin.lua:1164: in function 'run_global_rules'
           /opt/apisix/apisix/init.lua:403: in function 'common_phase'
           /opt/apisix/apisix/init.lua:747: in function 'http_body_filter_phase'
           body_filter_by_lua:2: in main chunk while sending to client, client: 
127.0.0.1, server: localhost, request: "GET /grpc_plus?a=1&b=2 HTTP/1.1", 
upstream: "grpc://127.0.0.1:50051", host: "localhost"
   2023/04/12 18:08:06 [warn] 2785045#2785045: *3 [lua] response.lua:190: 
hold_body_chunk(): create body_buffer: table: 0x7f4aede1e4c8 while sending to 
client, client: 127.0.0.1, server: localhost, request: "GET /grpc_plus?a=1&b=2 
HTTP/1.1", upstream: "grpc://127.0.0.1:50051", host: "localhost"
   2023/04/12 18:08:06 [warn] 2785045#2785045: *3 [lua] response.lua:191: 
hold_body_chunk(): init #chunk: 7 while sending to client, client: 127.0.0.1, 
server: localhost,request: "GET /grpc_plus?a=1&b=2 HTTP/1.1", upstream: 
"grpc://127.0.0.1:50051", host: "localhost"
   2023/04/12 18:08:06 [warn] 2785045#2785045: *3 [lua] response.lua:181: 
hold_body_chunk(): #chunk: 7 while sending to client, client: 127.0.0.1, 
server: localhost, request: "GET /grpc_plus?a=1&b=2 HTTP/1.1", upstream: 
"grpc://127.0.0.1:50051", host: "localhost"
   2023/04/12 18:08:06 [warn] 2785045#2785045: *3 [lua] response.lua:182: 
hold_body_chunk(): hold_body_chunk
   stack traceback:
           /opt/apisix/apisix/plugins/grpc-transcode.lua:202: in function 
'phase_func'
           /opt/apisix/apisix/plugin.lua:1131: in function 'common_phase'
           /opt/apisix/apisix/init.lua:747: in function 'http_body_filter_phase'
           body_filter_by_lua:2: in main chunk while sending to client, client: 
127.0.0.1, server: localhost, request: "GET /grpc_plus?a=1&b=2 HTTP/1.1", 
upstream: "grpc://127.0.0.1:50051", host: "localhost"
   2023/04/12 18:08:06 [warn] 2785045#2785045: *3 [lua] response.lua:196: 
hold_body_chunk(): add #chunk: 7 while sending to client, client: 127.0.0.1, 
server: localhost, request: "GET /grpc_plus?a=1&b=2 HTTP/1.1", upstream: 
"grpc://127.0.0.1:50051", host: "localhost"
   2023/04/12 18:08:06 [warn] 2785045#2785045: *3 [lua] response.lua:110: 
response(): #buffer: 14 while sending to client, client: 127.0.0.1, server: 
localhost, request: "GET /grpc_plus?a=1&b=2 HTTP/1.1", upstream: 
"grpc://127.0.0.1:50051", host: "localhost"
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to