Skip to content

Fix: clearSceneAndBustCache when rust panics because it can load a stale cache #6809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

nadr0
Copy link
Contributor

@nadr0 nadr0 commented May 9, 2025

closes #6808

Issue

When rust panics the rust cache can become stale and prevent you from rendering other KCL files in your project.

Implementation

  • added new custom unreachable panic error message matching
  • added generic stack track panic error message for rust runtime
  • clearSceneAndBustCache if rust panics to prevent the cache from not re executing when it needs to
  • Tried grabbing a specific enough keyword from app.zoo.dev when it crashes in the stack trace for the generic handler
    Screenshot from 2025-05-09 12-31-33

Video walkthrough

2025-05-09.12-26-07.mp4

KCL code that causes panic

/* Generated by Text-to-CAD:
Create a cube with sides of length 20mm.  On the top face, add a cylindrical boss of radius 6mm and height 2mm. Then, shell the body to 2mm thickness, leaving the top face of the cylindrical boss open. */
@settings(defaultLengthUnit = mm)

// Define the dimensions of the cube
cubeSide = 20

// Define the dimensions of the cylindrical boss
bossRadius = 6
bossHeight = 2

// Define the shell thickness
shellThickness = 2

// Define the fillet radius
filletRadius = 2

// Create a sketch for the cube
cubeSketch = startSketchOn(XY)
  |> startProfile(at = [-cubeSide / 2, -cubeSide / 2])
  |> xLine(length = cubeSide)
  |> yLine(length = cubeSide, tag = $seg01)
  |> xLine(length = -cubeSide)
  |> yLine(length = -cubeSide, tag = $seg02)
  |> close()

// Extrude the cube
cube = extrude(
       cubeSketch,
       length = cubeSide,
       tagEnd = $topFace,
       tagStart = $capStart001,
     )

// Apply fillets to all edges of the cube
filletedCube = fillet(
  cube,
  radius = filletRadius,
  tags = [
    getNextAdjacentEdge(seg01),
    getNextAdjacentEdge(seg02),
    getNextAdjacentEdge(getNextAdjacentEdge(seg01)),
    getNextAdjacentEdge(getNextAdjacentEdge(seg02))
  ]
)

// Create a sketch for the cylindrical boss on the top face of the cube
bossSketch = startSketchOn(filletedCube, face = topFace)
  |> circle(center = [0, 0], radius = bossRadius)

// Extrude the cylindrical boss
boss = extrude(bossSketch, length = bossHeight, tagEnd = $bossTopFace)

// Shell the cube, leaving the top face of the cylindrical boss open
shell([filletedCube, boss], thickness = shellThickness, faces = [bossTopFace])

Copy link

qa-wolf bot commented May 9, 2025

QA Wolf here! As you write new code it's important that your test coverage is keeping up.
Click here to request test coverage for this PR!

Copy link

vercel bot commented May 9, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
modeling-app ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 9, 2025 5:57pm

@nadr0 nadr0 changed the title fix: clear scene and bust cache if rust panics Fix: clearSceneAndBustCache when rust panics because it can load a stale cache May 9, 2025
@@ -2244,7 +2244,7 @@ w = f() + f()
|> line(end = [0, 0])
|> close()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what the fuck is this?

cargo fmt does nothing

Copy link
Contributor

Choose a reason for hiding this comment

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

It's inside a Rust string, so cargo fmt shouldn't modify your strings.

@nadr0 nadr0 requested review from jessfraz, a team and jtran May 9, 2025 17:39
@jessfraz
Copy link
Contributor

jessfraz commented May 9, 2025

ah smart! perfect use of that function but also why is rust panicing hahaa we should never panic

@nadr0
Copy link
Contributor Author

nadr0 commented May 9, 2025

hahaa we should never panic

Well that is something we are confused about. I've talk to Jon and Max about this and I've been able to come across 3 types of panics in Rust. That is why we have this global window event handler as well as the WASM module reload.

I took a valid main.kcl file, asked t2c edit to add fillets and the result produced a file that causes a rust panic. When rust panics its breaks out of the Javacript runtime which will prevent the cleanup workflow.

image
^--- this is the specific panic from the KCL code I listed above.

Seeing that this can randomly happen I want to get this catch all handler in place to prevent users from getting in an unrecoverable state and they only way to fix it is by refreshing.

The root cause has not been debugged on the Rust side. Since this is the 2nd one I've found while using the app I am worried we will have another oddly specific panic and then the user will have an unrecoverable state so I want to prevent that.

Copy link

codspeed-hq bot commented May 9, 2025

CodSpeed Instrumentation Performance Report

Merging #6809 will not alter performance

Comparing nadro/gh-6808/rust-panics-cache-bricks (a7ee874) with main (923fead)

Summary

✅ 54 untouched benchmarks

const stack = error?.error?.stack || null
if (typeof stack === 'string') {
const substringError = `WebAssembly.instantiate:wasm-function`
return stack.indexOf(substringError) !== -1 ? true : false
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return stack.indexOf(substringError) !== -1 ? true : false
return stack.indexOf(substringError) !== -1

@jtran
Copy link
Contributor

jtran commented May 10, 2025

Do we send error reports to the server to notify us when stuff like this gets triggered in the wild? If not, we should.

@jessfraz
Copy link
Contributor

@jtran we could add honeycomb tracing or something just wanna be mindful when we do we only track dire shit or we will get so much noise! but would be good to do!

@jessfraz
Copy link
Contributor

@jtran have you looked at that args.rs line hes talking about does it make sense why its panic-ing

@jessfraz
Copy link
Contributor

its this line:

Ok(T::from_kcl_val(&arg).unwrap())
we should make that not unwrap, i can fix

@jessfraz jessfraz mentioned this pull request May 10, 2025
@jessfraz
Copy link
Contributor

i opened a fix for the panic here: #6825
restarting the wasm isnt gooing to fix this, it will just panic again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG]: Asking t2c to fillet a box will produce a bad KCL and brick rust runtime
3 participants