Avoid a race condition in opt-viewer/optrecord (#131214)

See https://bugzilla.redhat.com/2336915
See https://reviews.llvm.org/D41784?id=
See
https://github.com/androm3da/optviewer-demo/issues/4#issuecomment-718787822

Fixes https://github.com/llvm/llvm-project/issues/62403.

The race condition happened when the demangler_proc was being set. The
locking mechanism itself happened too late.

This way, the lock always exists (to avoid a race when creating it) and
is always used when *creating* demangler_proc.
This commit is contained in:
Miro Hrončok 2025-04-02 11:52:41 +02:00 committed by GitHub
parent 9b2fd1a6ec
commit e0f8898e1d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -64,17 +64,19 @@ class Remark(yaml.YAMLObject):
default_demangler = "c++filt -n"
demangler_proc = None
demangler_lock = Lock()
@classmethod
def set_demangler(cls, demangler):
cls.demangler_proc = subprocess.Popen(
demangler.split(), stdin=subprocess.PIPE, stdout=subprocess.PIPE
)
cls.demangler_lock = Lock()
@classmethod
def demangle(cls, name):
with cls.demangler_lock:
if not cls.demangler_proc:
cls.set_demangler(cls.default_demangler)
cls.demangler_proc.stdin.write((name + "\n").encode("utf-8"))
cls.demangler_proc.stdin.flush()
return cls.demangler_proc.stdout.readline().rstrip().decode("utf-8")
@ -323,8 +325,6 @@ def get_remarks(input_file, filter_=None):
def gather_results(filenames, num_jobs, should_print_progress, filter_=None):
if should_print_progress:
print("Reading YAML files...")
if not Remark.demangler_proc:
Remark.set_demangler(Remark.default_demangler)
remarks = optpmap.pmap(
get_remarks, filenames, num_jobs, should_print_progress, filter_
)