Open
Description
I made an ad-hoc by-line process output reader and it seems that I found a memory leak.
I wrapped into a min working example below. This is supposed to uv.async_send
the lines of the output of a certain process. I setup a process that prints data endlessly. The output is stored in current_chunk
, but it's only temporary: each time data arrives, I look at the chunk and extract the lines, except for the last one (in case the last line did not arrive completely).
If you keep the uv.send_async
line below, memory consumption of the lua
process will grow endlessly. If you remove it, the memory consumption remains still. In both cases, each line is discarded.
local uv = require("luv") -- "luv" when stand-alone, "uv" in luvi apps-- WIP luv (related to luvit)
local stdout = uv.new_pipe()
local handle, pid =
uv.spawn(
"yes",
{stdio = {nil, stdout, nil},
args = {}
},
function(code,signal)
-- ignore
end)
local current_chunk = "" -- accumulated data until a \n
local no_more_output = false
-- process each line
cb = uv.new_async(function(line)
-- discard line
if no_more_output then
cb:close()
end
end)
uv.read_start(stdout, function(err, data)
assert(not err, err)
if data then
-- append received data into current_chunk
current_chunk = string.format("%s%s", current_chunk, data)
else
no_more_output= true
end
-- split chunk into lines to send to `callback`
lines={}
for line in string.gmatch(current_chunk, "[^\n]+") do
table.insert(lines, line)
end
if not no_more_output then
-- pop last chunk back into current chunk for next iteration
last_item_position = #lines
current_chunk = table.remove(lines, last_item_position)
end
-- send the lines to the callback
for _,l in pairs(lines) do
-- this line below creates a memory leak
uv.async_send(cb, l)
end
-- close uv stuff if everything's done
if no_more_output then
uv.async_send(cb, nil)
end
end)
uv.run()
Happens with these lua versions:
% lua -v
Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio
% luajit -v
LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/