[SERVER-68286] Avoid lifetime issues with mongo::mozjs::kCurrentScope Created: 26/Jul/22  Updated: 29/Oct/23  Resolved: 18/Jan/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 6.3.0-rc0

Type: Improvement Priority: Minor - P4
Reporter: Mohammad Dashti (Inactive) Assignee: Mohammad Dashti (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Backwards Compatibility: Fully Compatible
Sprint: 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
Participants:
Linked BF Score: 0

 Description   

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.



 Comments   
Comment by Githook User [ 22/Nov/22 ]

Author:

{'name': 'Mohammad Dashti', 'email': 'mdashti@gmail.com', 'username': 'mdashti'}

Message: SERVER-68286 Improved lifetime issues with `mongo::mozjs::currentJSScope`
Branch: master
https://github.com/mongodb/mongo/commit/b50819d5ffaeea1e195598b19d47bcc76f507b57

Generated at Thu Feb 08 06:10:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.