swc-project/swc · error · anyhow::Error

Failed to unwrap Arc: other references to transform_result e

Error message

Failed to unwrap Arc: other references to transform_result exist

What it means

Internal invariant failure in the plugin transform executor: after the plugin instance is dropped, the result buffer Arc<Mutex<Vec<u8>>> (shared with the plugin's host environment so set_transform_result can write into it) must have exactly one reference. Arc::try_unwrap failing means some component - the runtime's imported-function environment, a leaked instance handle, or a concurrent use of the same executor state - still holds a clone, so the transformed bytes cannot be safely extracted.

Source

Thrown at crates/swc_plugin_runner/src/transform_executor.rs:81

        let returned_ptr_result = self.instance.transform(
            guest_program_ptr.0,
            guest_program_ptr.1,
            unresolved_mark.as_u32(),
            should_enable_comments_proxy,
        )?;

        self.instance
            .caller()?
            .free(guest_program_ptr.0, guest_program_ptr.1)?;
        self.instance.cleanup()?;

        // Construct serialized struct from raw bytes.
        // Since we have finished transformation, it's safe to fetch the data from
        // Arc<Mutex<T>>
        drop(self.instance);
        let transformed_result = Arc::try_unwrap(self.transform_result)
            .map_err(|_| {
                anyhow!("Failed to unwrap Arc: other references to transform_result exist")
            })?
            .into_inner();
        let ret = PluginSerializedBytes::from_bytes(transformed_result);

        let ret = if returned_ptr_result == 0 {
            Ok(ret)
        } else {
            let err: PluginError = ret.deserialize()?.into_inner();
            match err {
                PluginError::SizeInteropFailure(msg) => Err(anyhow!(
                    "Failed to convert pointer size to calculate: {msg}"
                )),
                PluginError::Deserialize(msg) | PluginError::Serialize(msg) => {
                    Err(anyhow!("{msg}"))
                }
                _ => Err(anyhow!(
                    "Unexpected error occurred while running plugin transform"
                )),

View on GitHub (pinned to d7d7434666)

Solutions

  1. Upgrade swc/swc_core to the latest patch - teardown ordering bugs here get fixed quickly
  2. If you implement a custom runtime, verify your Instance drops all imported function environments (including the set_transform_result host env) so the Arc refcount reaches 1
  3. Avoid sharing one executor/instance across concurrent transforms; instantiate per task
  4. If it persists with the stock runtime, capture versions (swc_core, wasmer runtime) and report upstream
Defensive patterns

Strategy: try-catch

Try / catch

// Rust: this is an internal invariant failure - catch, log, and surface versions
match executor.run(...) {
    Err(e) if e.to_string().contains("other references to transform_result exist") => {
        tracing::error!("swc plugin teardown bug; swc_core={}, report upstream", env!("CARGO_PKG_VERSION"));
        return Err(e);
    }
    other => other,
}

Prevention

When it happens

Trigger: A custom runtime implementation that keeps function-environment references alive after instance drop, reusing/sharing TransformExecutor state across concurrent transforms, or a bug in plugin instance teardown. Not triggerable by malformed user input alone.

Common situations: Embedding swc_plugin_runner with a custom Runtime/Instance implementation that retains host environments; swc version regressions in teardown ordering; rarely, invoking the same plugin transform concurrently through one cached instance.

Related errors


AI-assisted analysis of swc-project/swc@d7d7434666 (2026-08-16). Data as JSON: /api/errors/00086a5d0b194134. Report an issue: GitHub.