openresty / openresty/lua-nginx-module

Tcp cosocket receiveuntil call mixed by other reading calls get unexpected result

Open
#2,131 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
11.8k
Forks
2.1k
Avg merge
6h 1m
Merged PRs (30d)
6

Description

If call receive after receiveuntil when met boundary pattern ambiguity, then call receiveuntil again will return a string which is not a valid subsequence of stream.

Software: v0.10.22 lua module

Config:

location /echo {
    return 200 -----abc--abc;
}

location /socket {
   default_type 'text/plain';
   content_by_lua_block {
       local sock = ngx.socket.tcp()
        
       ...
        
       local reader = sock:receiveuntil("--abc")
        
       local line, err, part = reader(2)
       ngx.say("read 1: ", "[", line, "]")
        
       line, err, part = sock:receive(4)
       ngx.say("read 2: ", "[", line, "]")
        
       line, err, part = reader()
       ngx.say("read 3: ", "[", line, "]")
    }
}

Output:

read 1: [-]
read 2: [--ab]
read 3: [--c]

After reading code, it seems that when some part of boundary pattern is consumed by other reading calls, the dfa doesn't reset state, so once find boundary pattern is not satisfy and copy data from cp->pattern will cause this problem (ngx_http_lua_socket_read_until->ngx_http_lua_socket_add_pending_data).
Since docs has mentioned that The iterator function can be called multiple times and can be mixed safely with other cosocket method calls or other iterator function calls., whether mixed call receiveuntil with receive is valid and the output is expected. Thanks for reply :).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the mixed receiveuntil and receive calls with the nginx configuration in the issue, then inspect ngx_http_lua_socket_read_until and ngx_http_lua_socket_add_pending_data. Compare the observed output with the documented promise that iterator calls can be mixed safely; done means the behavior is clarified and, if incorrect, corrected with coverage for this boundary-pattern case.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, lua
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.