Skip to content

fix(wasm): use per-engine cache dirs on Windows to avoid file lock error#624

Merged
ilblackdragon merged 7 commits intomainfrom
work-on-issue-448.-do-we-have
Mar 6, 2026
Merged

fix(wasm): use per-engine cache dirs on Windows to avoid file lock error#624
ilblackdragon merged 7 commits intomainfrom
work-on-issue-448.-do-we-have

Conversation

@zmanian
Copy link
Copy Markdown
Collaborator

@zmanian zmanian commented Mar 6, 2026

Summary

  • Fix Windows OS error 33 (ERROR_LOCK_VIOLATION) when multiple wasmtime engines share the default compilation cache directory. Each engine now gets its own cache subdirectory on Windows (wasmtime-tools/, wasmtime-channels/), avoiding file lock conflicts on memory-mapped cache files. Unix behavior is unchanged.
  • Add Windows CI jobs (cargo check + cargo clippy across all 3 feature flag combinations) to catch Windows-specific build issues going forward.

Details

The root cause: IronClaw creates two separate wasmtime Engine instances (one for tool WASM modules, one for channel WASM modules). Both called cache_config_load_default(), which uses a shared cache directory (~/.cache/wasmtime). On Windows, memory-mapped files get exclusive OS-level locks, so the second engine's Component::new() fails when it hits the lock from the first.

The Telegram channel is especially susceptible because it's typically the first channel set up after onboarding, creating a startup-load + immediate-hot-activation double-load scenario.

The fix introduces enable_compilation_cache() which:

  • On Unix: delegates to cache_config_load_default() (shared cache, no lock issues)
  • On Windows: writes a per-engine TOML config pointing to ~/.cache/ironclaw/wasmtime-{tools,channels}/

Both configs already had an unused cache_dir: Option<PathBuf> field which is now wired through as an explicit override on all platforms.

Test plan

  • cargo clippy --all --all-features -- zero warnings
  • cargo test --lib -- 1845 passed, 0 failed
  • CI: verify new Windows build + clippy jobs pass
  • Manual: Windows user confirms Telegram channel setup no longer errors

Closes #448

Generated with Claude Code

zmanian and others added 7 commits March 2, 2026 16:02
…ror (#448)

On Windows, multiple wasmtime Engine instances sharing the default
compilation cache directory hit OS error 33 (ERROR_LOCK_VIOLATION)
because Windows holds exclusive file locks on memory-mapped cache
files. This is especially triggered when the Telegram channel WASM
module is loaded at startup and then hot-activated via the Extensions
UI.

Fix by giving each engine its own cache subdirectory on Windows
(~/.cache/ironclaw/wasmtime-tools/ and wasmtime-channels/). On
Unix the shared default cache continues to work as before.

Also adds Windows CI jobs (cargo check + clippy across all feature
flag combinations) to catch Windows-specific issues going forward.

Closes #448

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Gate PathBuf import behind #[cfg(unix)] in container.rs (only used
in Unix socket path), suppress unused_mut on conflicts Vec in
channels.rs (mutations are platform-gated), and add cfg gates on
keychain constants and hex_to_bytes that are only used on macOS/Linux.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Use double-quoted TOML strings with backslash and double-quote
escaping for the cache directory path, preventing breakage or
injection when paths contain special characters (e.g. single
quotes on Unix, backslashes on Windows).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Fix import ordering in container.rs and line wrapping in runtime.rs
to pass the CI formatting check.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…merge main

- Gate `use std::path::PathBuf` with `#[cfg(unix)]` in sandbox/container.rs
  since all usages are inside `#[cfg(unix)]` functions, fixing Windows clippy
  error (unused import)
- Add two regression tests for `enable_compilation_cache` (#448): one verifies
  explicit cache directory creation and TOML config, another verifies label-based
  isolation between engines
- Resolve merge conflict in test.yml to include both windows-build and
  wasm-wit-compat jobs

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Path is used in non-cfg-gated functions (lines 148, 244) so it must
be available on all platforms. Only PathBuf is unix-specific.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions github-actions Bot added size: L 200-499 changed lines scope: channel/wasm WASM channel runtime scope: tool/wasm WASM tool sandbox scope: secrets Secrets management scope: setup Onboarding / setup scope: sandbox Docker sandbox scope: ci CI/CD workflows risk: high Safety, secrets, auth, or critical infrastructure contributor: core 20+ merged PRs and removed size: L 200-499 changed lines labels Mar 6, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical file locking issue on Windows that occurred when multiple Wasmtime engines attempted to use the same compilation cache directory. By isolating these caches per engine on Windows, the change prevents runtime errors and improves the stability of the application. It also enhances the development process by introducing dedicated CI checks for Windows, ensuring better cross-platform compatibility moving forward.

Highlights

  • Windows Cache Fix: Implemented per-engine compilation cache directories on Windows to resolve ERROR_LOCK_VIOLATION when multiple Wasmtime engines share the default cache.
  • Cross-Platform Cache Logic: Introduced a new enable_compilation_cache function to abstract cache configuration, applying platform-specific logic for Windows while maintaining existing behavior for Unix.
  • Windows CI Integration: Added new CI jobs for Windows (cargo check and cargo clippy) to proactively catch Windows-specific build issues.
Changelog
  • src/channels/wasm/runtime.rs
    • Updated the WASM channel runtime to use the new enable_compilation_cache function for configuring the Wasmtime compilation cache.
  • src/sandbox/container.rs
    • Restricted the PathBuf import to Unix-specific builds.
  • src/secrets/keychain.rs
    • Conditioned the SERVICE_NAME, MASTER_KEY_ACCOUNT constants and the hex_to_bytes function to compile only on macOS, Linux, or during tests.
  • src/setup/channels.rs
    • Added an #[allow(unused_mut)] attribute to suppress a potential warning in detect_existing_cloudflared.
  • src/tools/wasm/mod.rs
    • Exported the newly introduced enable_compilation_cache function from the runtime module.
  • src/tools/wasm/runtime.rs
    • Introduced a new enable_compilation_cache function to manage Wasmtime's compilation cache, including platform-specific logic for Windows.
    • Updated the WasmToolRuntime to utilize this new function.
    • Added two new unit tests to verify the per-engine cache directory isolation.
Ignored Files
  • Ignored by pattern: .github/workflows/** (2)
    • .github/workflows/code_style.yml
    • .github/workflows/test.yml
Activity
  • No human activity has occurred on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a file lock violation on Windows by introducing per-engine cache directories for wasmtime. The implementation correctly isolates cache directories for 'tools' and 'channels' engines on Windows while maintaining the existing shared cache behavior on other platforms. The changes are well-structured, moving the cache configuration logic into a shared function enable_compilation_cache. The addition of new tests for this function is also a great improvement.

My review includes one suggestion to improve the robustness of how the wasmtime cache configuration file is generated, by using the toml crate for serialization instead of manual string manipulation. Overall, this is a solid fix for the reported issue.

Comment thread src/tools/wasm/runtime.rs
Comment on lines +57 to +61
let escaped = dir
.to_string_lossy()
.replace('\\', "\\\\")
.replace('"', "\\\"");
let toml_content = format!("[cache]\nenabled = true\ndirectory = \"{}\"\n", escaped);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Manually escaping characters for TOML generation is brittle and can be error-prone if paths contain unusual characters. It's more robust to use the toml crate to handle serialization and escaping of values. This ensures that the generated TOML is always valid.

You can achieve this by using toml::Value::String, which will correctly quote and escape the path string. This may require adding the toml crate as a dependency if it's not already available.

Suggested change
let escaped = dir
.to_string_lossy()
.replace('\\', "\\\\")
.replace('"', "\\\"");
let toml_content = format!("[cache]\nenabled = true\ndirectory = \"{}\"\n", escaped);
let toml_content = format!(
"[cache]\nenabled = true\ndirectory = {}\n",
toml::Value::String(dir.to_string_lossy().into_owned())
);

@ilblackdragon ilblackdragon merged commit 13e000d into main Mar 6, 2026
44 checks passed
@ilblackdragon ilblackdragon deleted the work-on-issue-448.-do-we-have branch March 6, 2026 23:32
bkutasi pushed a commit to bkutasi/ironclaw that referenced this pull request Mar 28, 2026
…ror (nearai#624)

* fix(wasm): use per-engine cache dirs on Windows to avoid file lock error (nearai#448)

On Windows, multiple wasmtime Engine instances sharing the default
compilation cache directory hit OS error 33 (ERROR_LOCK_VIOLATION)
because Windows holds exclusive file locks on memory-mapped cache
files. This is especially triggered when the Telegram channel WASM
module is loaded at startup and then hot-activated via the Extensions
UI.

Fix by giving each engine its own cache subdirectory on Windows
(~/.cache/ironclaw/wasmtime-tools/ and wasmtime-channels/). On
Unix the shared default cache continues to work as before.

Also adds Windows CI jobs (cargo check + clippy across all feature
flag combinations) to catch Windows-specific issues going forward.

Closes nearai#448

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: silence Windows clippy warnings for platform-gated code

Gate PathBuf import behind #[cfg(unix)] in container.rs (only used
in Unix socket path), suppress unused_mut on conflicts Vec in
channels.rs (mutations are platform-gated), and add cfg gates on
keychain constants and hex_to_bytes that are only used on macOS/Linux.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: escape directory path in TOML cache config to prevent injection

Use double-quoted TOML strings with backslash and double-quote
escaping for the cache directory path, preventing breakage or
injection when paths contain special characters (e.g. single
quotes on Unix, backslashes on Windows).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: resolve cargo fmt formatting errors

Fix import ordering in container.rs and line wrapping in runtime.rs
to pass the CI formatting check.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix(ci): restore Path import for all platforms, keep PathBuf unix-only

Path is used in non-cfg-gated functions (lines 148, 244) so it must
be available on all platforms. Only PathBuf is unix-specific.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
drchirag1991 pushed a commit to drchirag1991/ironclaw that referenced this pull request Apr 8, 2026
…ror (nearai#624)

* fix(wasm): use per-engine cache dirs on Windows to avoid file lock error (nearai#448)

On Windows, multiple wasmtime Engine instances sharing the default
compilation cache directory hit OS error 33 (ERROR_LOCK_VIOLATION)
because Windows holds exclusive file locks on memory-mapped cache
files. This is especially triggered when the Telegram channel WASM
module is loaded at startup and then hot-activated via the Extensions
UI.

Fix by giving each engine its own cache subdirectory on Windows
(~/.cache/ironclaw/wasmtime-tools/ and wasmtime-channels/). On
Unix the shared default cache continues to work as before.

Also adds Windows CI jobs (cargo check + clippy across all feature
flag combinations) to catch Windows-specific issues going forward.

Closes nearai#448

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: silence Windows clippy warnings for platform-gated code

Gate PathBuf import behind #[cfg(unix)] in container.rs (only used
in Unix socket path), suppress unused_mut on conflicts Vec in
channels.rs (mutations are platform-gated), and add cfg gates on
keychain constants and hex_to_bytes that are only used on macOS/Linux.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: escape directory path in TOML cache config to prevent injection

Use double-quoted TOML strings with backslash and double-quote
escaping for the cache directory path, preventing breakage or
injection when paths contain special characters (e.g. single
quotes on Unix, backslashes on Windows).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: resolve cargo fmt formatting errors

Fix import ordering in container.rs and line wrapping in runtime.rs
to pass the CI formatting check.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix(ci): restore Path import for all platforms, keep PathBuf unix-only

Path is used in non-cfg-gated functions (lines 148, 244) so it must
be available on all platforms. Only PathBuf is unix-specific.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor: core 20+ merged PRs risk: high Safety, secrets, auth, or critical infrastructure scope: channel/wasm WASM channel runtime scope: ci CI/CD workflows scope: sandbox Docker sandbox scope: secrets Secrets management scope: setup Onboarding / setup scope: tool/wasm WASM tool sandbox

Projects

None yet

Development

Successfully merging this pull request may close these issues.

setting telegram bot error

2 participants