p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

[p5.js 2.0+ Bug Report]: p5.strands drops state updates made before break

Open bhabishnu opened this issue 1 month ago • 4 comments

Most appropriate sub-area of p5.js?

  • [ ] Accessibility
  • [ ] Color
  • [ ] Core/Environment/Rendering
  • [ ] Data
  • [ ] DOM
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [ ] Math
  • [ ] Typography
  • [ ] Utilities
  • [x] WebGL
  • [x] WebGPU
  • [x] p5.strands
  • [ ] Build process
  • [ ] Unit testing
  • [ ] Internationalization
  • [ ] Friendly errors
  • [ ] Other (specify if possible)

p5.js version

main (1fa9fd24a)

Web browser and version

Chromium

Operating system

Fedora Linux

Steps to reproduce this

A state update made immediately before break in a p5.strands loop can be lost.

For example:

let c = 0;

for (let i = 0; i < 4; i++) {
  c += 0.25;
  if (i >= 1) break;
}

Expected: c === 0.5
Actual: c === 0.25

This reproduces on both WebGL and WebGPU.

A smaller case shows the generated-code ordering:

if (i >= 1) {
  c = 0.5;
  break;
}

The generated GLSL contains the equivalent of:

if (T1 >= float(1.0000)) {
  float T5 = float(0.5000);
  break;
  T4 = T5;
}

WGSL has the same ordering, so the state write-back after break is unreachable.

As controls, both break before the mutation and the same mutation without break behave correctly on both backends.

I couldn't find an existing issue or PR covering this case.

bhabishnu avatar Oct 25 '26 14:10 bhabishnu

I noticed #9330 was opened before this issue was approved or assigned. I’d be happy to work on the fix once the approach is confirmed. Could a steward clarify how ownership should be handled here?

bhabishnu avatar Oct 26 '26 16:10 bhabishnu

We require people be assigned before opening PRs, feel free to ignore that one. Happy to assign this to you, thanks @bhabishnu! Also, while looking at blocks and this state management, do you notice anything similarly weird happening with calls to discard() in conditional or loop blocks?

davepagurek avatar Oct 27 '26 15:10 davepagurek

I checked discard() as well, including in conditionals and nested loops on both backends, and it seems to behave correctly. There are still some dead writes after discard, but they aren’t observable, so I don’t think there’s anything else to fix there.

bhabishnu avatar Oct 29 '26 11:10 bhabishnu

thanks for looking into it!

davepagurek avatar Oct 29 '26 12:10 davepagurek