Fork me on GitHub
#clojurescript
<
2024-03-26
>
Lidor Cohen10:03:10

Hi everyone 👋 I have a reagent app with some complicated code that runs into infinite recusrion somewhere (I don't know where). I'm usualy using flowstorm for debuging but the infinite recurion crashes the entire runtime and I can't access flowstorm recordings. Does anyone knows a way to stop the runtime from overloading itself (or anyother technique to debug this)?

jpmonettas11:03:55

hi! you can add a counter (an atom) which increments no a function and a throw when that counter goes over some threshold and then look at the recordings to see why it isn't stopping

jpmonettas11:03:56

I guess we should bring back the FlowStorm ability to not only stop recordings but also throw after a thread limit to break infinite loops/recursions

Lidor Cohen11:03:03

I tried a counter but the places I suspect didn't seem to work. I might be missing the loop.

jpmonettas11:03:08

you can always resort to a couple of console.logs to see that those parts are repeatedly being called, once you see there is repetition there you can :

(def cnt (atom 0))
(defn break-on-limit []
  (when (> cnt 1000) (throw (js/Error. "limit exceded")))
  (swap! cnt inc))

(defn one-of-your-functions []  
  (break-on-limit)
  
  ...)
or something like that

p-himik11:03:29

Here's what I do in similar situations: 1. Keep the DevTools panel open on the Sources tab (at least, that's what it's called in Chrome) 2. Trigger the recursion 3. Press the Pause button in the Sources tab 4. Check the call stack

🙏 1
jpmonettas11:03:20

that normally doesn't work for me @U2FRKM4TW on busy loops since it freezes the entire devtools

p-himik11:03:57

In Chrome as well? Because it definitely worked for me on busy loops, multiple times. Maybe not on for (;;) {} but when calling functions - definitely.

Lidor Cohen11:03:06

I'll give it a try

jpmonettas11:03:47

yeah that is the thing, it depends on the kind of loops, if you do a (loop [] ... (recur)) it doesn't work for me on Chrome nor Firefox

Lidor Cohen11:03:51

It looks like it somewhat worked I'm investigating the call stack now 🙏

jpmonettas11:03:50

oh nice! I guess I shouldn't completely discard that technique then

Lidor Cohen12:03:40

couldn't find the culcript though... I somehow figured out its related to some collection I'm trying to render for each child it enters an infinite recursion but I can't findout where \ how

jpmonettas12:03:56

can you cut the process with the counter and exception technique?

Lidor Cohen12:03:48

I can't seem to place the counter in the right place 😕

jpmonettas12:03:05

have you tried adding a (js/console.log ...) first to make sure that the code is going multiple times over there, then you could add the counter and throw the exception. Maybe your loop is related to events handling? so some action fires an event, which when handled does something that fires more events?

p-himik12:03:36

If every approach that you try fails, try at least to create a minimal reproducible example - I'd be curious to check it out.

jpmonettas12:03:35

in firefox the html inspector shows you all events registered on the dom, and you can break on them. Or in ClojureScript you could add a bunch of (js-debugger) statements, which will generate a javascript debugger; statement. Then you will be breaking everywhere, maybe that helps

jpmonettas13:03:37

if that doesn't work I can push pretty quickly a FlowStorm commit that will throw (instead of stop recording) after you set a limit. So there shouldn't be escape to that since all instrumented instructions will throw after the limit is reached and you should be able to explore recordings

Lidor Cohen13:03:24

The problem with my code is that it has a couple of layers and the bottom layer is basically a framework for the layers above it. While it passes a lot of tests and use-cases there are still (and probably always will be) some edge cases that causes it to break. Trying to debug a function there is very similar to trying to debug get , I hit it thousands of times before I get to the problamatic pattern. I'll try to reduce the context of the test case in order to debug it

Lidor Cohen13:03:45

Maybe I'll move my debugging efforts to the higher layers

jpmonettas13:03:33

let me push one commit to FlowStorm master that hopefully will help with that

jpmonettas13:03:40

@U8RHR1V60 ok, on master you have this now

jpmonettas13:03:37

so, if you check the throw on limit, all instrumented instructions will throw when the limit is reached

jpmonettas13:03:29

just tried it on ClojureScript also and seems to be working fine

Lidor Cohen14:03:40

🙏 I'll give it a try and let you know how it goes

👍 1
Lidor Cohen19:03:42

It worked, now I'm playing with the numbers to get enough into the loop but not enough to crash

jpmonettas19:03:15

great! happy bug hunting!

🙏 1