Skip to content

Commit 5b42bdd

Browse files
committed
fix(locker): remove unnecessary Enter/Exit in Locker::new()
The Enter+Exit calls before Locker::Initialize() were a no-op. The C++ Locker only checks IsLockedByCurrentThread() and RestoreThread(), neither of which depends on the entry stack. top_level_ is always true in our usage (no Locker/Unlocker nesting). Also fix incorrect doc comments claiming the isolate must be in "entered" state for raw::Locker::init().
1 parent de789a7 commit 5b42bdd

File tree

2 files changed

+3
-33
lines changed

2 files changed

+3
-33
lines changed

src/isolate.rs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,48 +2533,19 @@ impl AsRef<Isolate> for Isolate {
25332533
/// only on the thread where it was created. The underlying `UnenteredIsolate`
25342534
/// implements `Send`, allowing it to be transferred between threads, but a new
25352535
/// `Locker` must be created on each thread that needs to access the isolate.
2536-
///
2537-
/// # Panic Safety
2538-
///
2539-
/// `Locker::new()` is panic-safe. If a panic occurs during construction,
2540-
/// the isolate will be properly exited via a drop guard.
25412536
pub struct Locker<'a> {
25422537
raw: std::mem::ManuallyDrop<crate::scope::raw::Locker>,
25432538
isolate: &'a mut UnenteredIsolate,
25442539
}
25452540

2546-
/// Drop guard that calls `Isolate::Exit()` to undo the temporary Enter
2547-
/// in `Locker::new()`. On the success path, the guard is dropped normally
2548-
/// (calling Exit). On panic, Drop runs the same cleanup.
2549-
struct IsolateExitGuard(*mut RealIsolate);
2550-
2551-
impl Drop for IsolateExitGuard {
2552-
fn drop(&mut self) {
2553-
unsafe { v8__Isolate__Exit(self.0) };
2554-
}
2555-
}
2556-
25572541
impl<'a> Locker<'a> {
25582542
/// Creates a new `Locker` for the given isolate.
25592543
///
25602544
/// Acquires V8's per-isolate mutex. The isolate is **not** entered after
25612545
/// construction — use [`Locker::enter()`] to get an [`IsolateScope`].
25622546
pub fn new(isolate: &'a mut UnenteredIsolate) -> Self {
2563-
let isolate_ptr = isolate.cxx_isolate;
2564-
2565-
// Temporarily enter the isolate so the C++ Locker constructor sees
2566-
// IsCurrent()==true → sets top_level_=false → skips its own Enter/Exit.
2567-
unsafe { v8__Isolate__Enter(isolate_ptr.as_ptr()) };
2568-
2569-
// Guard: calls Exit on both success and panic paths.
2570-
let exit_guard = IsolateExitGuard(isolate_ptr.as_ptr());
2571-
2572-
// Initialize the C++ Locker (acts as pure mutex since top_level_=false).
25732547
let mut raw = unsafe { crate::scope::raw::Locker::uninit() };
2574-
unsafe { raw.init(isolate_ptr) };
2575-
2576-
// Undo the temporary Enter — isolate is now locked but not entered.
2577-
drop(exit_guard);
2548+
unsafe { raw.init(isolate.cxx_isolate) };
25782549

25792550
Self {
25802551
raw: std::mem::ManuallyDrop::new(raw),

src/scope/raw.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,7 @@ impl Drop for AllowJavascriptExecutionScope {
236236
/// the `Locker` is dropped. Dropping an uninitialized `Locker` is undefined
237237
/// behavior because `Drop` will call the C++ destructor on garbage data.
238238
///
239-
/// 2. **Isolate State**: The isolate passed to `init()` must be in "entered" state
240-
/// (via `v8::Isolate::Enter()`) before calling `init()`.
239+
/// 2. **Isolate Pointer**: The isolate pointer passed to `init()` must be valid.
241240
///
242241
/// 3. **Single Initialization**: `init()` must be called exactly once. Calling it
243242
/// multiple times is undefined behavior.
@@ -278,7 +277,7 @@ impl Locker {
278277
/// # Safety
279278
///
280279
/// - This must be called exactly once after `uninit()`
281-
/// - The isolate must be valid and in "entered" state
280+
/// - The isolate pointer must be valid
282281
/// - The isolate must not be locked by another `Locker`
283282
/// - After this call, the `Locker` owns the V8 lock until dropped
284283
#[inline]

0 commit comments

Comments
 (0)