Skip to content

Commit 5201cae

Browse files
committed
Avoiding most common race conditions
1 parent a788426 commit 5201cae

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

treecript/collector.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ def analyse_list_of_processes(
159159
attrs=[
160160
"name",
161161
"cpu_times",
162+
"memory_full_info",
163+
"io_counters",
162164
"net_connections",
163165
"pid",
164166
"cpu_percent",
@@ -168,10 +170,7 @@ def analyse_list_of_processes(
168170
"cmdline",
169171
"create_time",
170172
"status",
171-
"io_counters",
172173
"ppid",
173-
"memory_info",
174-
"memory_full_info",
175174
],
176175
)
177176

@@ -260,6 +259,7 @@ def execution_metrics_collector(
260259
timestamp_format: "str" = "%Y-%m-%d %H:%M:%S",
261260
match_docker: "bool" = False,
262261
) -> "pathlib.Path":
262+
# See https://stackoverflow.com/a/4791612
263263
pop = subprocess.Popen(cmdline, preexec_fn=os.setsid)
264264
# Just to be sure every descendant disappears
265265
atexit.register(
@@ -540,6 +540,14 @@ def process_metrics_collector(
540540
child_pid_str, create_time
541541
)
542542

543+
c_cpu = child_d["cpu_times"]
544+
c_mem = c_full_mem = child_d["memory_full_info"]
545+
c_io = child_d["io_counters"]
546+
c_conn = child_d["net_connections"]
547+
# The process suddenly disappeared
548+
if c_cpu is None or c_full_mem is None:
549+
continue
550+
543551
if mode_w == "w":
544552
logger.info(f"Writing data to CSV file {csv_filename.as_posix()}...")
545553

@@ -592,7 +600,6 @@ def process_metrics_collector(
592600
print(",".join(metrics_cols), file=cH)
593601

594602
# Counting TCP connections
595-
c_conn = child_d["net_connections"]
596603
tcp_connections = 0
597604
if c_conn is not None:
598605
for c_c in c_conn:
@@ -602,14 +609,10 @@ def process_metrics_collector(
602609
):
603610
tcp_connections += 1
604611

605-
c_cpu = child_d["cpu_times"]
606-
c_mem = child_d["memory_info"]
607612
c_mem_vms = c_mem.vms
608613
c_mem_rss = c_mem.rss
609-
c_full_mem = child_d["memory_full_info"]
610614
c_full_mem_uss = getattr(c_full_mem, "uss", 0)
611615
c_full_mem_swap = getattr(c_full_mem, "swap", 0)
612-
c_io = child_d["io_counters"]
613616
metrics = (
614617
timestamp_str,
615618
child_pid_str,
@@ -633,12 +636,12 @@ def process_metrics_collector(
633636
" ".join(map(str, child_d["threads_core_num"])),
634637
" ".join(map(str, child_d["threads_cpu_num"])),
635638
str(child_d["status"]),
636-
str(c_io.read_count),
637-
str(c_io.write_count),
638-
str(c_io.read_bytes),
639-
str(c_io.write_bytes),
640-
str(c_io.read_chars),
641-
str(c_io.write_chars),
639+
str(getattr(c_io, "read_count", 0)),
640+
str(getattr(c_io, "write_count", 0)),
641+
str(getattr(c_io, "read_bytes", 0)),
642+
str(getattr(c_io, "write_bytes", 0)),
643+
str(getattr(c_io, "read_chars", 0)),
644+
str(getattr(c_io, "write_chars", 0)),
642645
)
643646

644647
# Aggregated statistics

0 commit comments

Comments
 (0)