-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Avoid a race condition in opt-viewer/optrecord #131214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
See https://bugzilla.redhat.com/2336915 See https://reviews.llvm.org/D41784?id= See androm3da/optviewer-demo#4 (comment) Fixes llvm#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. I don't have any prior experience with the codebase and I cannot guarantee the behavior is correct. It appears to get rid of the race (which I was seeing consistently). multiprocessing.pool.RemoteTraceback: """ Traceback (most recent call last): File "/usr/lib64/python3.14/multiprocessing/pool.py", line 125, in worker result = (True, func(*args, **kwds)) ~~~~^^^^^^^^^^^^^^^ File "/usr/lib64/python3.14/multiprocessing/pool.py", line 48, in mapstar return list(map(*args)) File "/builddir/build/BUILD/llvm-19.1.6-build/llvm-project-19.1.6.src/llvm/tools/opt-viewer/optpmap.py", line 25, in _wrapped_func return func(argument, filter_) File "/builddir/build/BUILD/llvm-19.1.6-build/llvm-project-19.1.6.src/llvm/tools/opt-viewer/opt-viewer.py", line 293, in _render_file SourceFileRenderer(source_dir, output_dir, filename, no_highlight).render(remarks) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^ File "/builddir/build/BUILD/llvm-19.1.6-build/llvm-project-19.1.6.src/llvm/tools/opt-viewer/opt-viewer.py", line 216, in render self.render_source_lines(self.source_stream, line_remarks) ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/builddir/build/BUILD/llvm-19.1.6-build/llvm-project-19.1.6.src/llvm/tools/opt-viewer/opt-viewer.py", line 123, in render_source_lines self.render_inline_remarks(remark, html_line) ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^ File "/builddir/build/BUILD/llvm-19.1.6-build/llvm-project-19.1.6.src/llvm/tools/opt-viewer/opt-viewer.py", line 126, in render_inline_remarks inlining_context = r.DemangledFunctionName ^^^^^^^^^^^^^^^^^^^^^^^ File "/builddir/build/BUILD/llvm-19.1.6-build/llvm-project-19.1.6.src/llvm/tools/opt-viewer/optrecord.py", line 153, in DemangledFunctionName return self.demangle(self.Function) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^ File "/builddir/build/BUILD/llvm-19.1.6-build/llvm-project-19.1.6.src/llvm/tools/opt-viewer/optrecord.py", line 77, in demangle with cls.demangler_lock: ^^^^^^^^^^^^^^^^^^ AttributeError: type object 'Passed' has no attribute 'demangler_lock'. Did you mean: 'demangler_proc'? """
@@ -64,17 +64,19 @@ class Remark(yaml.YAMLObject): | |||
|
|||
default_demangler = "c++filt -n" | |||
demangler_proc = None | |||
demangler_lock = Lock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I 100% agree with that.
|
||
@classmethod | ||
def demangle(cls, name): | ||
with cls.demangler_lock: | ||
if not cls.demangler_proc: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this really need to be within the Lock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the underlying problem is that before that patch, caller had to ensure set_demangler
was called before calling demangle
.
A neater way to enforce that would be to make demangler_proc
lazily initialized, but the proposed approach is fine as demangler_proc
is only referenced from demangle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When it's outside of the lock, two workers could attempt to create the process.
- worker A sees demangler_proc is None
- worker B sees demangler_proc is None
- worked A assigns a new process to demangler_proc
- worker B assigns a new process to demangler_proc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was even getting stuff like:
File "/builddir/build/BUILD/llvm-20.1.0-build/llvm-project-20.1.0.src/llvm/tools/opt-viewer/optrecord.py", line 160, in DemangledFunctionName
return self.demangle(self.Function)
~~~~~~~~~~~~~^^^^^^^^^^^^^^^
File "/builddir/build/BUILD/llvm-20.1.0-build/llvm-project-20.1.0.src/llvm/tools/opt-viewer/optrecord.py", line 85, in demangle
cls.demangler_proc.stdin.write((name + "\n").encode("utf-8"))
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'stdin'
/cherry-pick e0f8898 |
@hroncok Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/162/builds/19363 Here is the relevant piece of the build log for the reference
|
/pull-request #134058 |
See https://bugzilla.redhat.com/2336915 See https://reviews.llvm.org/D41784?id= See androm3da/optviewer-demo#4 (comment) Fixes llvm#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. (cherry picked from commit e0f8898)
See https://bugzilla.redhat.com/2336915
See https://reviews.llvm.org/D41784?id=
See androm3da/optviewer-demo#4 (comment)
Fixes #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.
I don't have any prior experience with the codebase and I cannot guarantee the behavior is correct.
It appears to get rid of the race (which I was seeing consistently).