Skip to content

Commit c6652fc

Browse files
Ignore Errno::ESRCH.
1 parent 9a8de28 commit c6652fc

2 files changed

Lines changed: 18 additions & 18 deletions

File tree

lib/process/metrics/memory/linux.rb

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,16 @@ class Memory::Linux
1111
# @parameter pid [Integer] The process ID.
1212
# @parameter usage [Memory] The Memory instance to populate with fault counters.
1313
def self.capture_faults(pid, usage)
14-
begin
15-
stat = File.read("/proc/#{pid}/stat")
16-
# The comm field can contain spaces and parentheses; find the closing ')':
17-
rparen_index = stat.rindex(")")
18-
return unless rparen_index
19-
fields = stat[(rparen_index+2)..-1].split(/\s+/)
20-
# proc(5): field 10=minflt, 12=majflt; our fields array is 0-indexed from field 3.
21-
usage.minor_faults = fields[10-3].to_i
22-
usage.major_faults = fields[12-3].to_i
23-
rescue Errno::ENOENT, Errno::EACCES
24-
# The process may have exited or permissions are insufficient; ignore.
25-
rescue => error
26-
# Be robust to unexpected formats; ignore errors silently.
27-
end
14+
stat = File.read("/proc/#{pid}/stat")
15+
# The comm field can contain spaces and parentheses; find the closing ')':
16+
rparen_index = stat.rindex(")")
17+
return unless rparen_index
18+
fields = stat[(rparen_index+2)..-1].split(/\s+/)
19+
# proc(5): field 10=minflt, 12=majflt; our fields array is 0-indexed from field 3.
20+
usage.minor_faults = fields[10-3].to_i
21+
usage.major_faults = fields[12-3].to_i
22+
rescue
23+
# Be robust to unexpected formats; ignore errors silently.
2824
end
2925

3026
# @returns [Numeric] Total memory size in kilobytes.
@@ -72,8 +68,8 @@ def self.capture(pid, **options)
7268
usage.map_count += File.readlines("/proc/#{pid}/maps").size
7369
# Also capture fault counters:
7470
self.capture_faults(pid, usage)
75-
rescue Errno::ENOENT => error
76-
# Ignore.
71+
rescue Errno::ENOENT, Errno::ESRCH
72+
# Ignore, process may have ended.
7773
end
7874

7975
return usage
@@ -104,8 +100,8 @@ def self.capture(pid, **options)
104100
end
105101
# Also capture fault counters:
106102
self.capture_faults(pid, usage)
107-
rescue Errno::ENOENT => error
108-
# Ignore.
103+
rescue Errno::ENOENT, Errno::ESRCH
104+
# Ignore, process may have ended.
109105
end
110106

111107
return usage

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Handle `Errno::ESRCH: No such process @ io_fillbuf - fd:xxx /proc/xxx/smaps_rollup` by ignoring it.
6+
37
## v0.6.0
48

59
- Add support for major and minor page faults on Linux: `Process::Metrics::Memory#major_faults` and `#minor_faults`. Unfortunately these metrics are not available on Darwin (macOS).

0 commit comments

Comments
 (0)