Skip to content

Commit

Permalink
fix: improve isolate error handling (#1451)
Browse files Browse the repository at this point in the history
<!-- Please make sure there is an issue that this PR is correlated to. -->
Fixes RVT-4173
Fixes RVT-4175
Fixes RVT-4178
Fixes RVT-4180
## Changes

<!-- If there are frontend changes, please include screenshots. -->
  • Loading branch information
MasterPtato committed Nov 23, 2024
1 parent f4e2f42 commit 47d01df
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 123 deletions.
19 changes: 19 additions & 0 deletions docs-internal/infrastructure/pegboard/ISOLATE_RUNNER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Error handling

There are several layers of error handling in the isolate runner. Going from the isolate runtime to the main
thread:

1. Js runtime errors are caught in various places in `run_inner` in `src/isolate.rs`. These are written to the
stderr log stream and visible to users on the dashboard. No error is thrown from the function.
2. Errors besides js runtime errors are thrown by `run_inner` and caught by `run`. An error line is written to
the stderr log stream stating that a fatal error has occurred. An error code of 1 is returned by the `run`
function and no error is thrown by `run`.
3. If any error is thrown by the `run` function (likely during setup or cleanup), it is caught by the tokio
task (in `src/main.rs`) which is watching the thread where the `run` function is running from. Upon any
error, it logs the error and sends a message to the `fatal_tx` channel.
4. The `main` function in `src/main.rs` is a `tokio::select!` on the WS retry loop and `fatal_tx`. If any
message is received on `fatal_tx`, an error code of 1 is written and the program throws "Fatal error".
5. Besides messages from `fatal_tx`, the main function can fail during setup (redirecting logs, reading
config, writing pid, etc) or from fatal errors from the WS connection. This includes bad packets or failed
socket sends. It is intended to fail for these cases, but should automatically handle retryable errors like
the socket closing.
50 changes: 25 additions & 25 deletions packages/infra/client/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/infra/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ git = "https://github.com/rivet-gg/sqlx"
rev = "e7120f59"

[workspace.dependencies.deno_runtime]
# path = "../../../../../deno/runtime"
git = "https://github.com/rivet-gg/deno"
rev = "1f423e6d87792cb761cf0167c7bad5b8553f32a1"

rev = "fcb9ac39b25219b9ce51c13442b577e5d2be61ab"
2 changes: 1 addition & 1 deletion packages/infra/client/isolate-v8-runner/js/40_rivet_kv.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ function deserializeKey(key) {
if ("inKey" in key || "outKey" in key) {
let jsKey = key.inKey ?? key.outKey;

let tuple = jsKey[0].map((x) => core.deserialize(x));
let tuple = jsKey.map((x) => core.deserialize(x));

if (tuple.length == 1) return tuple[0];
else return tuple;
Expand Down
12 changes: 11 additions & 1 deletion packages/infra/client/isolate-v8-runner/src/ext/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,24 @@ deno_core::extension!(
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
enum Key {
// TODO: See below comment
#[allow(dead_code)]
InKey(Vec<JsBuffer>),
OutKey(Vec<ToJsBuffer>),
}

impl From<actor_kv::key::Key> for Key {
fn from(value: actor_kv::key::Key) -> Self {
match value {
actor_kv::key::Key::JsInKey(tuple) => Key::InKey(tuple),
// TODO: Currently, JsBuffer cannot be serialized back to v8 as a buffer. We must convert it until
// fixed.
// actor_kv::key::Key::JsInKey(tuple) => Key::InKey(tuple),
actor_kv::key::Key::JsInKey(tuple) => Key::OutKey(
tuple
.into_iter()
.map(|buf| ToJsBuffer::from(Box::from(&*buf)))
.collect(),
),
actor_kv::key::Key::JsOutKey(tuple) => {
Key::OutKey(tuple.into_iter().map(Into::into).collect())
}
Expand Down
Loading

0 comments on commit 47d01df

Please sign in to comment.