Skip to content

Commit

Permalink
Always close profiler output file.
Browse files Browse the repository at this point in the history
Reported by Guilherme Batalheiro.

(cherry picked from commit fca6633)
  • Loading branch information
Mike Pall authored and ligurio committed Feb 10, 2025
1 parent 1ca1736 commit 89484f5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/jit/p.lua
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,7 @@ local function prof_finish()
local samples = prof_samples
if samples == 0 then
if prof_raw ~= true then out:write("[No samples collected]\n") end
return
end
if prof_ann then
elseif prof_ann then
prof_annotate(prof_count1, samples)
else
prof_top(prof_count1, prof_count2, samples, "")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
local tap = require('tap')
local test = tap.test('lj-1304-close-profile-dump-with-0-samples'):skipcond({
['Test requires /proc filesystem'] = jit.os == 'OSX',
})
local utils = require('utils')

test:plan(1)

local p_filename = '/tmp/profile'

-- <makecmd> runs %testname%/script.lua by <LUAJIT_TEST_BINARY>
-- with the given environment, launch options and CLI arguments.
local script = utils.exec.makecmd(arg)
-- Execute a Lua script with start and stop LuaJIT profiler,
-- it is expected no samples found by profiler. The script's
-- output is suppressed, it is not interested.
local _ = script(p_filename)

local p_content = io.open(p_filename):read('a*')
test:is(utils.tools.trim(p_content), '[No samples collected]', 'profile dump has no samples')

-- Teardown.
os.remove(p_filename)

test:done(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local p_options = 'ri1'
local p_filename = assert(arg[1], 'filename argument is missing')

require('jit.p').start(p_options, p_filename)

-- No code to generate profiling samples.

-- Stop profiler to execute `jit/p.lua:prof_fmt()`. With zero
-- samples it triggers early return without closing the file.
require('jit.p').stop()

-- Make sure LuaJIT profiler is close a file handle.
local ls_output = io.popen('ls -l /proc/$$/fd'):read('a*')
assert(ls_output:find(p_filename) == nil, 'file is open')
4 changes: 4 additions & 0 deletions test/tarantool-tests/utils/tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ function M.read_file(path)
return content
end

function M.trim(str)
return (str:gsub('^%s*(.-)%s*$', '%1'))
end

return M

0 comments on commit 89484f5

Please sign in to comment.