Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8351484

Race condition in max stats in MonitorList::add

XMLWordPrintable

    • master

      Here:
      https://github.com/openjdk/jdk/blob/f61f520e699e3eb5104c9467ec8269b837da74db/src/hotspot/share/runtime/synchronizer.cpp#L79-L82

        size_t count = Atomic::add(&_count, 1u);
        if (count > max()) {
          Atomic::inc(&_max);
        }

      This code is actually racy. Here is one of the awkward traces:

      (assume count = 0, max = 0 initially)

      Thread 1:
        size_t count = Atomic::add(&_count, 1u);
        // count is 1, _count is 1

      Thread 2:
        size_t count = Atomic::add(&_count, 1u);
        // count is 2, _count is 2
        if (count > max()) { // TRUE
           Atomic::inc(&_max);
           // _max is now 1
        }

      Thread 1: (resumes)
        // local count is still 1, but _max is now 1
        if (count > max()) { // FALSE
          ...
        }

      End result: _count = 2, _max = 1

            shade Aleksey Shipilev
            shade Aleksey Shipilev
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: