Skip to content

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

Merged
merged 1 commit into from
Apr 2, 2025
Merged

Conversation

hroncok
Copy link
Contributor

@hroncok hroncok commented Mar 13, 2025

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).

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'?
"""

Copy link

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 @ followed by their GitHub username.

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'?
    """
@vstinner
Copy link

cc @serge-sans-paille

@serge-sans-paille serge-sans-paille self-requested a review March 20, 2025 14:25
@@ -64,17 +64,19 @@ class Remark(yaml.YAMLObject):

default_demangler = "c++filt -n"
demangler_proc = None
demangler_lock = Lock()
Copy link
Collaborator

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:
Copy link
Collaborator

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?

Copy link
Collaborator

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

Copy link
Contributor Author

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.

  1. worker A sees demangler_proc is None
  2. worker B sees demangler_proc is None
  3. worked A assigns a new process to demangler_proc
  4. worker B assigns a new process to demangler_proc

Copy link
Contributor Author

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'

@nikic nikic merged commit e0f8898 into llvm:main Apr 2, 2025
9 of 11 checks passed
@nikic nikic added this to the LLVM 20.X Release milestone Apr 2, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Apr 2, 2025
@nikic
Copy link
Contributor

nikic commented Apr 2, 2025

/cherry-pick e0f8898

Copy link

github-actions bot commented Apr 2, 2025

@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!

@hroncok hroncok deleted the race branch April 2, 2025 09:56
@llvm-ci
Copy link
Collaborator

llvm-ci commented Apr 2, 2025

LLVM Buildbot has detected a new failure on builder lldb-x86_64-debian running on lldb-x86_64-debian while building llvm at step 6 "test".

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
Step 6 (test) failure: build (failure)
...
PASS: lldb-api :: functionalities/tail_call_frames/cross_object/TestCrossObjectTailCalls.py (435 of 2806)
PASS: lldb-api :: functionalities/breakpoint/breakpoint_options/TestBreakpointOptions.py (436 of 2806)
PASS: lldb-api :: tools/lldb-dap/locations/TestDAP_locations.py (437 of 2806)
PASS: lldb-api :: lang/cpp/constructors/TestCppConstructors.py (438 of 2806)
PASS: lldb-api :: functionalities/breakpoint/auto_continue/TestBreakpointAutoContinue.py (439 of 2806)
PASS: lldb-api :: commands/expression/call-function/TestCallStdStringFunction.py (440 of 2806)
PASS: lldb-api :: lang/cpp/fpnan/TestFPNaN.py (441 of 2806)
PASS: lldb-api :: commands/platform/process/launch/TestPlatformProcessLaunch.py (442 of 2806)
PASS: lldb-api :: functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/TestCoroutineHandle.py (443 of 2806)
PASS: lldb-api :: functionalities/thread/concurrent_events/TestConcurrentSignalNWatchNBreak.py (444 of 2806)
FAIL: lldb-api :: functionalities/thread/jump/TestThreadJump.py (445 of 2806)
******************** TEST 'lldb-api :: functionalities/thread/jump/TestThreadJump.py' FAILED ********************
Script:
--
/usr/bin/python3 /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./lib --env LLVM_INCLUDE_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/include --env LLVM_TOOLS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./bin --arch x86_64 --build-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex --lldb-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/lldb --compiler /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/clang --dsymutil /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./bin --lldb-obj-root /home/worker/2.0.1/lldb-x86_64-debian/build/tools/lldb --lldb-libs-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./lib -t /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/functionalities/thread/jump -p TestThreadJump.py
--
Exit Code: -11

Command Output (stdout):
--
lldb version 21.0.0git (https://github.com/llvm/llvm-project.git revision e0f8898e1d432ab4fdcaf353bf87a1d3cf36491a)
  clang revision e0f8898e1d432ab4fdcaf353bf87a1d3cf36491a
  llvm revision e0f8898e1d432ab4fdcaf353bf87a1d3cf36491a
Skipping the following test categories: ['libc++', 'dsym', 'gmodules', 'debugserver', 'objc']

--
Command Output (stderr):
--
Change dir to: /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/functionalities/thread/jump
UNSUPPORTED: LLDB (/home/worker/2.0.1/lldb-x86_64-debian/build/bin/clang-x86_64) :: test_dsym (TestThreadJump.ThreadJumpTestCase.test_dsym) (test case does not fall in any category of interest for this run) 
runCmd: settings clear -all

output: 

runCmd: settings set symbols.enable-external-lookup false

output: 

runCmd: settings set target.inherit-tcc true

output: 

runCmd: settings set target.disable-aslr false

output: 

runCmd: settings set target.detach-on-error false

output: 

@llvmbot
Copy link
Member

llvmbot commented Apr 2, 2025

/pull-request #134058

@llvmbot llvmbot moved this from Needs Triage to Done in LLVM Release Status Apr 2, 2025
swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request Apr 11, 2025
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

AttributeError: type object 'Missed' has no attribute 'demangler_lock' running opt-viewer
6 participants