Skip to content

Commit 2ccff07

Browse files
committed
prevent busy waits and log errors
1 parent 3330925 commit 2ccff07

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

cpp_linter/clang_tools/__init__.py

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import Optional, List, Dict, Tuple, cast
77
import shutil
88

9-
from ..common_fs import FileObj
9+
from ..common_fs import FileObj, FileIOTimeout
1010
from ..common_fs.file_filter import TidyFileFilter, FormatFileFilter
1111
from ..loggers import start_log_group, end_log_group, worker_log_init, logger
1212
from .clang_tidy import run_clang_tidy, TidyAdvice
@@ -45,34 +45,52 @@ def _run_on_single_file(
4545
args: Args,
4646
) -> Tuple[str, str, Optional[TidyAdvice], Optional[FormatAdvice]]:
4747
log_stream = worker_log_init(log_lvl)
48+
filename = Path(file.name).as_posix()
4849

49-
format_advice = None
50+
format_advice = FormatAdvice(filename)
5051
if format_cmd is not None and (
5152
format_filter is None or format_filter.is_source_or_ignored(file.name)
5253
):
53-
format_advice = run_clang_format(
54-
command=format_cmd,
55-
file_obj=file,
56-
style=args.style,
57-
lines_changed_only=args.lines_changed_only,
58-
format_review=args.format_review,
59-
)
54+
try:
55+
format_advice = run_clang_format(
56+
command=format_cmd,
57+
file_obj=file,
58+
style=args.style,
59+
lines_changed_only=args.lines_changed_only,
60+
format_review=args.format_review,
61+
)
62+
except FileIOTimeout: # pragma: no cover
63+
logger.error(
64+
"Failed to read or write contents of %s when running clang-format",
65+
filename,
66+
)
67+
except OSError: # pragma: no cover
68+
logger.error(
69+
"Failed to open the file %s when running clang-format", filename
70+
)
6071

61-
tidy_note = None
72+
tidy_note = TidyAdvice([])
6273
if tidy_cmd is not None and (
6374
tidy_filter is None or tidy_filter.is_source_or_ignored(file.name)
6475
):
65-
tidy_note = run_clang_tidy(
66-
command=tidy_cmd,
67-
file_obj=file,
68-
checks=args.tidy_checks,
69-
lines_changed_only=args.lines_changed_only,
70-
database=args.database,
71-
extra_args=args.extra_arg,
72-
db_json=db_json,
73-
tidy_review=args.tidy_review,
74-
style=args.style,
75-
)
76+
try:
77+
tidy_note = run_clang_tidy(
78+
command=tidy_cmd,
79+
file_obj=file,
80+
checks=args.tidy_checks,
81+
lines_changed_only=args.lines_changed_only,
82+
database=args.database,
83+
extra_args=args.extra_arg,
84+
db_json=db_json,
85+
tidy_review=args.tidy_review,
86+
style=args.style,
87+
)
88+
except FileIOTimeout: # pragma: no cover
89+
logger.error(
90+
"Failed to Read/Write contents of %s when running clang-tidy", filename
91+
)
92+
except OSError: # pragma: no cover
93+
logger.error("Failed to open the file %s when running clang-tidy", filename)
7694

7795
return file.name, log_stream.getvalue(), tidy_note, format_advice
7896

cpp_linter/common_fs/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ def read_with_timeout(self, timeout_ns: int = 1_000_000_000) -> bytes:
184184
if f.readable():
185185
contents = f.read()
186186
success = True
187+
else: # pragma: no cover
188+
time.sleep(0.001)
187189
except OSError as exc: # pragma: no cover
188190
exception = exc
189191
if not success and exception: # pragma: no cover
@@ -222,11 +224,14 @@ def read_write_with_timeout(
222224
original_data = f.read()
223225
f.seek(0)
224226
else: # pragma: no cover
227+
time.sleep(0.001)
225228
continue
226229
while not success and time.monotonic_ns() < timeout:
227230
if f.writable():
228231
f.write(data)
229232
success = True
233+
else: # pragma: no cover
234+
time.sleep(0.001)
230235
except OSError as exc: # pragma: no cover
231236
exception = exc
232237
if not success and exception: # pragma: no cover

0 commit comments

Comments
 (0)