Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-68286

Avoid lifetime issues with mongo::mozjs::kCurrentScope

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 6.3.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • None
    • Fully Compatible
    • QE 2022-09-19, QE 2022-10-03, QE 2022-10-17, QE 2022-10-31, QE 2022-11-14, QE 2022-11-28, QE 2022-12-12, QE 2022-12-26, QE 2023-01-09, QE 2023-01-23
    • 0

      First, a `thread_local` storage object like this

      thread_local std::atomic<MozJSImplScope*> kCurrentScope = nullptr;  // NOLINT
      

      should be disqualified from being called a k- constant as it has a subtle and dynamic lifecycle.

      Second, the only use of this variable is as the returned pointer from getThreadScope.
      And the only use of getThreadScope is to call setOOM() on it.

              auto scope = mongo::mozjs::MozJSImplScope::getThreadScope();
              if (scope)
                  scope->setOOM();
      

      `scope` goes out of scope after this statement. No more statements in enclosing block.

      One solution is to convert the thread_local to a class that has a definite destructor, similar to how it's done for ThreadName and the same technique should work here.

            Assignee:
            mohammad.dashti@mongodb.com Mohammad Dashti (Inactive)
            Reporter:
            mohammad.dashti@mongodb.com Mohammad Dashti (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: