{"record":{"id":"a94e1830c936a4d3","repo":"glzr-io/glazewm","slug":"cannot-run-command-because-subject-container-is-de","errorCode":null,"errorMessage":"Cannot run command because subject container is detached.","messagePattern":"Cannot run command because subject container is detached\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/wm/src/wm.rs","lineNumber":230,"sourceCode":"    }\r\n\r\n    Ok(current_subject_container.id())\r\n  }\r\n\r\n  #[allow(clippy::too_many_lines)]\r\n  pub fn run_command(\r\n    command: &InvokeCommand,\r\n    subject_container: Container,\r\n    state: &mut WmState,\r\n    config: &mut UserConfig,\r\n  ) -> anyhow::Result<()> {\r\n    // No-op if WM is currently paused.\r\n    if state.is_paused && *command != InvokeCommand::WmTogglePause {\r\n      return Ok(());\r\n    }\r\n\r\n    if subject_container.is_detached() {\r\n      bail!(\"Cannot run command because subject container is detached.\");\r\n    }\r\n\r\n    match &command {\r\n      InvokeCommand::AdjustBorders(args) => {\r\n        match subject_container.as_window_container() {\r\n          Ok(window) => {\r\n            let args = args.clone();\r\n            let border_delta = RectDelta::new(\r\n              args.left.unwrap_or(LengthValue::from_px(0)),\r\n              args.top.unwrap_or(LengthValue::from_px(0)),\r\n              args.right.unwrap_or(LengthValue::from_px(0)),\r\n              args.bottom.unwrap_or(LengthValue::from_px(0)),\r\n            );\r\n\r\n            window.set_border_delta(border_delta);\r\n            state.pending_sync.queue_container_to_redraw(window);\r\n\r\n            Ok(())\r","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/glzr-io/glazewm/blob/5709ad0a3c7c386bbc3e38166a865ffc12937515/packages/wm/src/wm.rs#L212-L248","documentation":"`Wm::run_command` refuses to operate on a container that has been detached from the window-management tree. Detached containers (closed windows, removed workspaces, nodes pending cleanup) have no valid position or siblings, so mutating them is undefined. The command is aborted with this bail to protect tree invariants.","triggerScenarios":"An `InvokeCommand` arrives whose subject container was detached between selection and execution — e.g. the target window closed after focus moved, a race between an event handler detaching the container and the command processor acting on a stale handle, or a cached `ContainerRef` reused after a relayout.","commonSituations":"IPC/automation scripts sending commands to a window handle that was just closed; keybindings firing right after the focused window exits; async command queues holding stale container references across event-loop ticks.","solutions":["Check `subject_container.is_detached()` before invoking the command and skip or re-resolve the target.","Re-resolve the target from current state (e.g. focused window) at command execution time instead of caching it.","If driven by IPC, validate that the window/container ID still exists before sending the command.","Handle gracefully: treat the bail as a no-op for transient races (window closed concurrently) rather than a fatal error.","Ensure detach/cleanup paths don't leave stale references in queues or focus history."],"exampleFix":"// before\nwm.run_command(state, &command, &subject_container).await?;\n// after\nif subject_container.is_detached() {\n  tracing::warn!(\"Skipping command; subject container detached.\");\n  return Ok(());\n}\nwm.run_command(state, &command, &subject_container).await?;","handlingStrategy":"validation","validationCode":"fn command_applicable(c: &Container) -> bool { !c.is_detached() }","typeGuard":"fn as_attached(c: &Container) -> Option<&Container> { if c.is_detached() { None } else { Some(c) } }","tryCatchPattern":"match wm.run_command(state, &cmd, &subject).await {\n  Err(e) if e.to_string().contains(\"detached\") => tracing::warn!(\"Target detached; skipping\"),\n  other => other?,\n}","preventionTips":["Re-resolve the target container from current state at execution time.","Don't cache ContainerRefs across event-loop ticks or await points.","Filter out detached containers when queuing commands."],"tags":["rust","window-manager","stale-reference"],"backgroundTag":"invalid-state-transition","analyzedSha":"5709ad0a3c7c386bbc3e38166a865ffc12937515","analyzedAt":"2026-09-08T03:26:40.288Z","contentChangedAt":"2026-09-08T03:26:40.288Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}