参数传递后出现意外变化

运行环境:lua 5.1和openresty 1.17.8.2在x86_64下编译。

当我执行以下代码时,发生了奇怪的错误:

-- code.lua
local function _render_ini_file(agent_gen, uid, instance_data)
    local context = {}
    return agent_gen._render(agent_gen, 'realnode_ini', context, _get_realnode_ini_file(uid))
end

-- agent_gen.lua
local function _render(self, category, context, file)
    ngx_log(ngx_NOTICE, 'Render ', category, ' to ', file)
    return file_ex.write(file, self.renders[category].render(context))
end

-- file_ex.lua
function _M.write(filename, content, append)
    ngx_log(ngx_ERR, 'write filename: ', filename)
    local is_exist = libc_ex.file_exist(filename)
    local mode = append and 'a+' or 'w'
    local file, err = io.open(filename, mode)
    if not file then
        return _log_and_return_err('Failed to open ' .. filename .. ' for write: ' .. err)
    end
    if not is_exist then
        libc_ex.chmod(filename, libc_ex.S_IRWUSER)
    end

    local ok, err = file:write(content)
    if not ok then
        file:close()
        return _log_and_return_err('Failed to write ' .. filename .. ': ' .. err)
    end
    file:close()
    ngx_log(ngx_INFO, 'Write file ', filename, ' successful')
    return true
end

并且以以下日志为例:

[notice] 25539#0: *16220 [lua] agent_gen.lua:45: _render_ini_file(): Render realnode_ini to /config/realnode.ini, context: ngx.timer
[error] 25539#0: *16220 [lua] file_ex.lua:42: _render_ini_file(): write filename: /config/realnode.ini, context: ngx.timer
[error] 25539#0: *16220 [lua] file_ex.lua:42: write(): write filename: /config/, context: ngx.timer

好像字符串的值在传递参数时自己改变了。此外,错误级别的日志打印了两次。有什么想法吗?

原文链接 https://stackoverflow.com/questions/69881450

点赞