Macaulay2 / M2

The primary source code repository for Macaulay2, a system for computing in commutative algebra, algebraic geometry and related fields.
https://macaulay2.com
340 stars 228 forks source link

try fails to catch alarm #3371

Open pzinn opened 1 month ago

pzinn commented 1 month ago

There are already existing open issues with alarm, this I think is a new one:

i1 : try (alarm 1; sleep 15;) else <<"should end up here (and does)"<<endl;
should end up here (and does)

i2 : try (alarm 1; sleep 15) else <<"should end up here (and doesn't)"<<endl;
stdio:2:51:(3): error: alarm occurred

This issue is presumably related to tail call optimisation (?). Note that I already submitted a PR a while ago about tail call, namely https://github.com/Macaulay2/M2/pull/3144, but it does not impact the issue described above.

I traced the issue to two different places that alarm is triggered, both in evaluate.d, namely evalraw in the first case, evalexcept in the second. I could try to fix something but first I would need to understand better the difference between these two functions. Note that evalraw has this comment

      if test(exceptionFlag) && !steppingFurther(c) then (    -- compare this code to the code in evalexcept() below

presumably suggesting I should notice something that I don't...

mahrud commented 1 month ago

To make sure I understand, the problem is that try isn't catching the errors occurred during TCO, right? e.g. things look fine without try:

i27 : f = t -> (alarm 1; sleep t;); f 10
stdio:23:38:(3): error: alarm occurred

i29 : f = t -> (alarm 1; sleep t); f 10
stdio:24:37:(3): error: alarm occurred

I'm sure you've seen it, but the solution might be related to #2596 also.

pzinn commented 1 month ago

I don't think this is the same bug as #2596, though experience fixing one might help with the other.

pzinn commented 1 month ago

@DanGrayson (or anyone else) can you explain why eval(f:Frame,c:Code) calls evalexcept: https://github.com/Macaulay2/M2/blob/ec9e9ac60ed4a8e791448942202f077a88a87e15/M2/Macaulay2/d/evaluate.d#L1616-L1621 and not just eval(c:Code)? I guess I'm asking the same question as above, what is the purpose of evalexcept?