{"record":{"id":"28cdd2d86a5b1b18","repo":"zed-industries/zed","slug":"arena-end-scope-called-without-a-matching-begin-s","errorCode":null,"errorMessage":"Arena::end_scope called without a matching begin_scope","messagePattern":"Arena::end_scope called without a matching begin_scope","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/gpui/src/arena.rs","lineNumber":130,"sourceCode":"\n    /// Marks the start of a scope (e.g. a window draw) whose allocations must stay\n    /// live until the scope ends, even if `clear` is called by a nested scope in\n    /// the meantime.\n    pub fn begin_scope(&mut self) {\n        self.scope_depth += 1;\n    }\n\n    /// Ends the innermost scope started with `begin_scope`.\n    ///\n    /// Panics if no scope is active: an unbalanced `end_scope` would let `clear`\n    /// run while an enclosing scope still references arena memory, which is\n    /// exactly the use-after-free this bookkeeping exists to prevent, so failing\n    /// loudly here is preferable.\n    pub fn end_scope(&mut self) {\n        self.scope_depth = self\n            .scope_depth\n            .checked_sub(1)\n            .expect(\"Arena::end_scope called without a matching begin_scope\");\n    }\n\n    /// Drops all allocations and resets the arena, unless a scope is still active.\n    ///\n    /// When a draw triggers a nested draw (e.g. re-entrant window procedure\n    /// invocations on Windows, or opening a window from within a draw), the nested\n    /// draw's clear must not free memory the outer draw still references, so it is\n    /// deferred: the outer draw's own clear will drop both draws' allocations.\n    pub fn clear(&mut self) {\n        if self.scope_depth == 0 {\n            self.force_clear();\n        } else {\n            log::debug!(\n                \"deferring arena clear; {} enclosing scope(s) still active\",\n                self.scope_depth\n            );\n        }\n    }","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/zed-industries/zed/blob/f4178619acd0d47ea1f76a2025c42962c6d6638c/crates/gpui/src/arena.rs#L112-L148","documentation":"Arena::end_scope decrements scope_depth and panics when it is already zero: end_scope was called without a matching begin_scope. An unbalanced end would let clear() free memory an enclosing scope still references, causing use-after-free, so the arena fails loudly instead.","triggerScenarios":"Thrown at crates/gpui/src/arena.rs:130 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Ensure every end_scope is paired with exactly one begin_scope on the same code path","Check early returns/panics between begin and end that skip the pairing","Wrap scope usage in a guard type that ends the scope on drop"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"f4178619acd0d47ea1f76a2025c42962c6d6638c","analyzedAt":"2026-08-20T19:29:52.058Z","contentChangedAt":"2026-08-20T19:29:52.058Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}