Different Cloud vs. Desktop Return behavior in V13.1

After install V13.1 one of my scripts get broke in my Mac. Below is the core of the bug:

Module[{}, Return[1]; 2]

Return1

In the Wolfram Cloud we get the expected result.

Cross post in Stack Exchange

I can confirm that this bug also occurs on 13.0.0 for Linux (64-bit), see this attached screenshot:

With versions 5.2, 8.0.4 and 12.3.1 on Windows 10 x64 I get the same result:

Interesting! It works at Wolfram Cloud

Here is a duplicate question on MMa.SE with a detailed answer from Leonid Shifrin:

Read also this (the last section “Why did Return appear?”) and this.

Hey guys,

*2 things:

    1. not everything is a BUG, I myself fell into a lot of traps in Return problems when I create functions in WL, due to lack of training thro documentation (my fault), but (I believe that) nothing about this command has changed in recent years.. I recommend testing through the Documentation!
    1. Wolfram’s QA (quality assurance) is the best of the best developers, they are amazing!
      ..for me, Return is working correctly in 13.1, I don’t know if something has changed internally, because I didn’t identify it..

..regarding the inconsistency between Cloud and Mathematica, I believe a ticket has already been made..

Thanks!

Claudio Chaib

The unexpected behavior exhibited by the cloud can be explained with something like

Attributes[myevaluateFunction] = {HoldAllComplete};
myevaluateFunction[input___] := WithCleanup[setup\[\], input, teardown\[\]\];

CloudEvaluate could very well be using something similar to the code above. Now if you put the module in this evaluation wrapper you get what you initially expected:

In[32]:= myevaluateFunction[Module[{}, Return[1]; 2]]

Out[32]= 1

I imagine this is the same thing happening with CloudEvaluate[Module[{}, Return[1]; 2]] or just running the Module in a cloud notebook: there is some wrapper function that takes your code and passes it to the kernel and then returns the results to you. This extra layer is why the Return[1] works.

So if you ever find yourself wanting to write code like

Module[{}, Return[1]; 2]

stop and write

Catch[Module[{}, Throw[1]; 2]

instead.