Skip to content

Commit 2221f53

Browse files
committed
process: handle possible fio.read errors
Once I catched an error during tests: ``` tarantool/override/metrics/psutils/psutils_linux.lua:49: bad argument #1 to 'string.split' (string expected, got nil) ``` That shows that in some rare cases fio.read can return `nil, err` but before this patch this case was unhandled. This patch fixes it. Also it was pretty strange to print a message via "print" function. It was changed to "log" module that is more native for tarantool. This patch doesn't contain tests because it's pretty hard to reproduce such error (file was opened but `file:read()` returned an errors for some reasons). Closes #526
1 parent d58e4d2 commit 2221f53

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
### Fixed
1818

19+
- Fixed a potential `process` metrics error when `fio.read` returns an empty string or an error.
20+
1921
# [1.5.0] - 2025-08-13
2022

2123
### Added

metrics/psutils/psutils_linux.lua

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local fio = require('fio')
22
local string = require('string')
33
local ffi = require('ffi')
4+
local log = require('log')
45

56
local get_nprocs_conf = function() end
67
if jit.os == 'Linux' then
@@ -19,7 +20,7 @@ local function get_cpu_time()
1920

2021
local stats_raw = cpu_stat_file:read(512)
2122
cpu_stat_file:close()
22-
if #stats_raw == 0 then
23+
if stats_raw == nil or #stats_raw == 0 then
2324
return nil
2425
end
2526

@@ -37,14 +38,22 @@ local function get_cpu_time()
3738
end
3839

3940
local function parse_process_stat(path)
40-
local stat = fio.open(path, 'O_RDONLY')
41-
if stat == nil then
42-
print('stat open error')
41+
local stat, err = fio.open(path, 'O_RDONLY')
42+
if err ~= nil then
43+
log.error('stat open error: %s', tostring(err))
4344
return nil
4445
end
4546

46-
local s = stat:read(512)
47+
local s
48+
s, err = stat:read(512)
4749
stat:close()
50+
if err ~= nil then
51+
log.error('stat read error: %s', tostring(err))
52+
return nil
53+
end
54+
if s == nil or #s == 0 then
55+
return nil
56+
end
4857

4958
local stats = string.split(s)
5059
return {

0 commit comments

Comments
 (0)