frontend icon indicating copy to clipboard operation
frontend copied to clipboard

Infinite loop when evaluating circular references

Open CZX123 opened this issue 1 year ago • 8 comments

A circular reference inside the playground code leads to an infinite loop, crashing the Source Academy. An example can be seen by running the below code:

const a = [];
a[0] = a;

This affects all available runners of all TS, JS, and Scheme variants, except Source §3 Concurrent (which is based on a VM).

The console error appears to be caused by "immer" module, but the error is logged from SafeEffects.ts:

CZX123 avatar Mar 27 '24 07:03 CZX123

The console error appears to be caused by "immer" module

Can you elaborate? I can't immediately see why Immer would be causing the error.

RichDom2185 avatar Mar 28 '24 12:03 RichDom2185

Now it's giving me a slightly different error: It seems like Immer is still handling the context objects like environment trees when the CSE Machine gets run, which results in this error as there are definitely circular references inside.

CZX123 avatar Mar 29 '24 04:03 CZX123

It seems like Immer is still handling the context objects like environment trees when the CSE Machine gets run, which results in this error as there are definitely circular references inside.

I see, the problem is with the design of the app: the js-slang context should have never been part of the frontend state in the first place since the golden rule of redux is to always ensure that state is immutable. The fact that js-slang context is mutable makes us resort to a lot of stuff like disabling auto-freeze and other workarounds.

Hmm, we really need to refactor code evaluation soon….

@sayomaki @JoelChanZhiYang sorry to ping you out of nowhere but do you have any thoughts?

RichDom2185 avatar Mar 29 '24 04:03 RichDom2185

See also: https://immerjs.github.io/immer/pitfalls/

RichDom2185 avatar Mar 29 '24 04:03 RichDom2185

Just to clarify, is the circular reference inside the js-slang context intended behavior?

Or in other words, is the root cause of the bug the fact that js-slang has a circular reference? Or is it because immer does not allow of circular references?

JoelChanZhiYang avatar Mar 29 '24 06:03 JoelChanZhiYang

Just to clarify, is the circular reference inside the js-slang context intended behavior?

Yes.

Or in other words, is the root cause of the bug the fact that js-slang has a circular reference? Or is it because immer does not allow of circular references?

I'd say both are intended behavior, i.e. the root cause is actually the fact that we have the execution context (which is mutable) being put in redux (which expects everything to be immutable), thereby violating Redux's golden rule, causing the incompatiblity when we move to modern redux (aka RTK, with Immer).

Immer was just doing its job.

RichDom2185 avatar Mar 29 '24 07:03 RichDom2185

Ah I see. The context seems to be used all over the place. But it seems like it is mostly being consumed rather than modified. It is modified in UPDATE_SUBLANGUAGE and I assume when the various eval functions are called.

How do you foresee the refactor to look like? Will the entire js-slang context still be stored? If so, where will it be stored?

JoelChanZhiYang avatar Mar 29 '24 08:03 JoelChanZhiYang

Actually, the error only occurs when the code that is written in the playground editor has the circular reference, it seems like the rest of the js-slang context is still fine despite also having circular references, maybe that could give a clue?

Edit: seems like source 3 non-det also does not work, and will crash regardless of what is written in the editor.

CZX123 avatar Mar 29 '24 11:03 CZX123

@tsammeow will this problem go away with conductor?

martin-henz avatar Mar 26 '25 09:03 martin-henz

this issue will likely go away after migrating to conductor framework

tsammeow avatar Apr 06 '25 05:04 tsammeow