Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
68dddfd
Give Rust APIs the IoContext of the current request
bartlomieju Oct 24, 2026
2596ce1
Merge remote-tracking branch 'origin/main' into biwanczuk/rust-jsg-io…
bartlomieju Oct 26, 2026
c93f799
Fix clippy errors in the io-context test bridge
bartlomieju Oct 26, 2026
0ac8d6d
Say what to change in both copies of the no-request message
bartlomieju Oct 26, 2026
497ff59
Create Rust builtin modules' exports outside any request
bartlomieju Oct 26, 2026
9ffcd92
Merge remote-tracking branch 'origin/main' into biwanczuk/rust-jsg-io…
bartlomieju Oct 27, 2026
0e9a43b
Run the io-context crate's unit tests
bartlomieju Oct 27, 2026
d137485
Merge branch 'main' into biwanczuk/rust-jsg-io-context
bartlomieju Oct 28, 2026
802490c
Merge branch 'main' into biwanczuk/rust-jsg-io-context
bartlomieju Oct 28, 2026
934cd23
Merge branch 'main' into biwanczuk/rust-jsg-io-context
bartlomieju Oct 28, 2026
12d6f72
Merge remote-tracking branch 'origin/main' into biwanczuk/rust-jsg-io…
bartlomieju Oct 28, 2026
60e6022
Get the IoContext id and error message through the shim
bartlomieju Oct 28, 2026
af4b59c
Merge remote-tracking branch 'origin/biwanczuk/rust-jsg-io-context' i…
bartlomieju Oct 28, 2026
2a91b05
Merge branch 'main' into biwanczuk/rust-jsg-io-context
bartlomieju Oct 28, 2026
839ce65
Make jsg::Error::new_internal_error() public
bartlomieju Oct 27, 2026
42efd1c
Add await_io() and add_object() to the Rust io-context crate
bartlomieju Oct 27, 2026
998fc4f
Merge remote-tracking branch 'origin/main' into biwanczuk/rust-io-con…
bartlomieju Oct 29, 2026
3d56e23
Handle await_io() failures and check IoOwned access
bartlomieju Oct 29, 2026
0efc9ad
Count await_io()'s JsExceptionThrown in the allowlist
bartlomieju Oct 29, 2026
b394f3f
Merge remote-tracking branch 'origin/main' into biwanczuk/rust-io-con…
bartlomieju Oct 30, 2026
dd7c015
Rename IoContext::await_io to await_io_and_run
bartlomieju Oct 30, 2026
5275f1f
Merge remote-tracking branch 'origin/main' into biwanczuk/rust-io-con…
bartlomieju Oct 30, 2026
744da07
Point the Rust JSG docs at the await_io_and_run() exception and tests
bartlomieju Oct 30, 2026
8bc245e
Define the io-context test bridge's C++ callbacks in a library
bartlomieju Oct 30, 2026
49d533e
Merge branch 'main' into biwanczuk/rust-io-context-await-io
bartlomieju Oct 30, 2026
3ec326e
Merge branch 'main' into biwanczuk/rust-io-context-await-io
bartlomieju Oct 30, 2026
fc74e46
Merge branch 'main' into biwanczuk/rust-io-context-await-io
bartlomieju Oct 30, 2026
9df424d
Merge remote-tracking branch 'origin/main' into biwanczuk/rust-io-con…
bartlomieju Oct 30, 2026
f042712
Have awaitIoAndRun() fail the task when its promise fails
bartlomieju Oct 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/rust/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ _Snapshot — the set drifts as crates come and go; `bazel query //src/rust/...`
- **Rust JSG safety rules** (full text and examples in `src/rust/jsg/README.md` §Safety rules):
1. Never throw while execution is terminating, and never discard a `jsg::Error` from a call that ran JavaScript or from `ToJS::to_js`, which can fail; pass it on with `?`.
2. Never put traced state (`TracedGlobal`, `jsg::Rc`, or a type holding them) behind `std::rc::Rc`. To give a callback or another party access, give it a `jsg::Rc` or `jsg::Weak` to the resource that owns the state (a method gets one as `this: jsg::Rc<Self>`). Clone a `jsg::Rc` right after moving it out of a traced field (`take`, `replace`, `pop`), since the moved one is neither strong nor traced ([#7976](https://github.com/cloudflare/workerd/issues/7976)). Don't put a `Global` in anything GC traces.
3. The C++ shims call V8 directly, not C++ JSG (`JSG_TRY`, `jsg::check`, `jsg::Lock::from`, `createTunneledException`, `makeInternalError`, `jsg::Data`, ...). `//src/rust/jsg:cpp-jsg-allowlist-test` enforces this for the files in `CPP_SHIMS` in `jsg/BUILD.bazel`; add shim files outside `src/rust/jsg` there.
3. The C++ shims call V8 directly, not C++ JSG (`JSG_TRY`, `jsg::check`, `jsg::Lock::from`, `createTunneledException`, `makeInternalError`, `jsg::Data`, ...). The one reviewed exception is `io_context_await_io_and_run()` in `src/workerd/io/io-context/ffi.c++`, which throws `jsg::JsExceptionThrown` to pass a pending exception to `IoContext::run()`; don't extend it. `//src/rust/jsg:cpp-jsg-allowlist-test` enforces this for the files in `CPP_SHIMS` in `jsg/BUILD.bazel`; add shim files outside `src/rust/jsg` there.
4. `Local<'s>` and `Lock<'s>` stay in their scope, and futures passed to async I/O are `'static`.
- **Formatting**: `rustfmt.toml` — `group_imports = "StdExternalCrate"`, `imports_granularity = "Item"` (one `use` per import)
- **Linting**: `just clippy <crate>` for a crate here, `just clippy <label>` for one elsewhere — pedantic+nursery; `allow-unwrap-in-tests`
Expand Down

This file was deleted.

13 changes: 0 additions & 13 deletions src/rust/jsg-compile-tests/await-io-future-is-static.rs

This file was deleted.

27 changes: 27 additions & 0 deletions src/rust/jsg-test/tests/internal_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2026 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

//! An internal error reaches JavaScript without its message.

use jsg::FromJS;

#[test]
fn internal_error_is_redacted_when_thrown() {
crate::Harness::new().run_in_context(|lock, _ctx| {
let thrown =
lock.try_catch(|_lock| Err::<(), _>(jsg::Error::new_internal_error("secret detail")))?;
let Err(exception) = thrown else {
panic!("the internal error was not thrown");
};
let err = jsg::Error::from_js(lock, exception)?;
assert_eq!(err.name, jsg::ExceptionType::Error);
assert!(
err.message.starts_with("internal error; reference = "),
"{}",
err.message
);
assert!(!err.message.contains("secret detail"), "{}", err.message);
Ok(())
});
}
1 change: 1 addition & 0 deletions src/rust/jsg-test/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod gc;
mod global;
mod harness;
mod integer;
mod internal_error;
mod isolates;
mod jsg_oneof;
mod jsg_oneof_variants;
Expand Down
10 changes: 7 additions & 3 deletions src/rust/jsg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ of a field; reviewers check those.
C++ JSG will be removed, so Rust JSG must not depend on it. New code in the C++ shims in
`src/rust/jsg` calls V8 directly (`v8::TryCatch`, `v8::MaybeLocal`, `isolate->ThrowException`) and
leaves decisions to Rust. Some existing shims still use C++ JSG. Those uses are listed in the
allowlist described below, and are to be removed.
allowlist described below, and are to be removed. The one exception is in
`src/workerd/io/io-context/ffi.c++`, where `io_context_await_io_and_run()` throws
`JsExceptionThrown` to pass an exception that Rust left pending to `IoContext::run()`, which
learns of exceptions no other way. Don't extend it.

Don't add C++ JSG to the shims. This includes `jsg::Lock` (including `Lock::from`),
`JSG_TRY`/`JSG_CATCH`/`tryCatch`, `jsg::check`/`JsExceptionThrown`, `createTunneledException`,
Expand Down Expand Up @@ -163,8 +166,9 @@ and `.h` file in `src/rust/jsg`. To check a shim file in another package, export
resource field. Both can be dropped without the lock, and after the isolate is gone.

Tested by: the compile tests in `src/rust/jsg-compile-tests` (`local-*`, `lock-*`, `callback-*`,
`await-io-*`, `global-*`, `traced-global-*`) and the drop tests in
`src/rust/jsg-test/tests/global.rs` and `traced_global_lifetime.rs`.
`global-*`, `traced-global-*`) and `src/workerd/io/io-context/compile-tests`
(`await-io-and-run-*`), and the drop tests in `src/rust/jsg-test/tests/global.rs` and
`traced_global_lifetime.rs`.

## Resources

Expand Down
6 changes: 5 additions & 1 deletion src/rust/jsg/cpp-jsg-allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# C++ JSG will be removed, so these counts may only go down (src/rust/jsg/README.md, "Safety
# rules", rule 3). Don't raise a count or add a line. When you remove a use, lower its count,
# and delete the line when it reaches zero.
# and delete the line when it reaches zero. The uses at the end are a reviewed exception.
#
# Format: <path from the repository root> <construct> <count>

Expand All @@ -11,3 +11,7 @@ src/rust/jsg/ffi.c++ makeInternalError 1
src/rust/jsg/jsg.h jsg::Lock 3
src/rust/jsg/jsg.h JsExceptionThrown 1
src/rust/jsg/jsg.h jsg::Data 1

# A reviewed exception: io_context_await_io_and_run() propagates the exception that a failed
# callback left pending to IoContext::run() as JsExceptionThrown, which is how run() learns of it.
src/workerd/io/io-context/ffi.c++ JsExceptionThrown 1
30 changes: 7 additions & 23 deletions src/rust/jsg/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

use std::future::Future;
use std::num::ParseIntError;
use std::ops::Deref;
use std::pin::Pin;
Expand Down Expand Up @@ -459,15 +458,14 @@ impl Error {
})
}

/// Like `new_type_error()`, but marks the resulting `Error` `is_internal`, so
/// `Lock::throw_exception()` redacts `message` instead of exposing it to guest JS.
/// The JS-visible error type ends up as plain `Error` regardless of the type set
/// here (`Lock::throw_internal_error()` -> C++ `makeInternalError()` always throws
/// a plain `Error`), so `name` only matters if something inspects the `Error`
/// value directly without throwing it (e.g. in tests).
fn new_internal_error(message: impl Into<String>) -> Self {
/// An error that only a bug causes, like a failed C++ `KJ_ASSERT`. Throwing it
/// (`Lock::throw_exception()`) redacts `message`: JavaScript gets a generic
/// `"internal error; reference = <id>"` `Error`, and `message` is logged with that ID.
/// `name` is `Error`, the type JavaScript gets (`Lock::throw_internal_error()` -> C++
/// `makeInternalError()`).
pub fn new_internal_error(message: impl Into<String>) -> Self {
Self {
name: ExceptionType::TypeError,
name: ExceptionType::Error,
message: message.into(),
is_internal: true,
pending: false,
Expand Down Expand Up @@ -696,20 +694,6 @@ impl<'s> Lock<'s> {
unsafe { v8::ffi::isolate_throw_exception(self.isolate().as_ffi(), error.into_ffi()) }
}

/// Runs `fut` as I/O and later calls `callback` with its output under a new lock. Both run
/// after the current callback has returned, so they must be `'static`.
#[expect(
clippy::unimplemented,
reason = "`await_io` is not implemented for Rust resources yet"
)]
pub fn await_io<F, C, I, R>(&mut self, _fut: F, _callback: C) -> Result<R>
where
F: Future<Output = I> + 'static,
C: for<'t> FnOnce(&mut Lock<'t>, I) -> Result<R> + 'static,
{
unimplemented!("Lock::await_io is not yet implemented for Rust resources")
}

pub(crate) fn realm(&mut self) -> &mut Realm {
// SAFETY: isolate is valid and locked (guaranteed by Lock); Realm pointer is valid.
unsafe { &mut *crate::ffi::realm_from_isolate(self.isolate().as_ffi()) }
Expand Down
Loading
Loading