Bug introduced in 8 or earlier and persisting through 13.0.1

I am using Mathematica 11.3 and this seems to me to be a bug. I would like, if possible, some idea on a workaround.

Here is an example of a trivial code that works as expected:

nI = 10;
(NM[#] :=  Print[#] ) & /@ Range[1, nI];

That is, the code above generates no output, as expected.

Now, if the first line is changed to

nI = 20;

The same code leads to 40 lines being printed! From 1 to 20 two times.

For some reason, the DistributeDefinitions is forcing the definition of NM to be executed, and I do not want that to happen before I use ParallelSubmit and WaitAll. I tried this in two computers with Mathematica 11.3, any ideas on what is happening?

  • 2
    $\begingroup$ On my machine the behaviour changes from nI=17 to 18. If I look at Definitions[NM], up to 17 it prints as :=-definitions, but above that it prints as =-definitions. Thus the core issue may be not with the parallel tools, but with how definitions are stored. $\endgroup$
    – Szabolcs
    Dec 28, 2018 at 20:58
  • 3
    $\begingroup$ The reason appears to be that Language`ExtendedFullDefinition switches from using RuleDelayed to Rule after 18 down-values. This causes DistributeDefinitions to subsequently leak the evaluation. $\endgroup$
    – Lukas Lang
    Dec 28, 2018 at 20:58
  • 1
    $\begingroup$ Thanks for the suggestion. I already sent an email to wolfram support, I am wating for their reply. $\endgroup$
    – Davi
    Jan 1, 2019 at 16:08
  • 1
    $\begingroup$ I confirm this is still a bug in M12 $\endgroup$ May 15, 2019 at 14:31
  • 1
    $\begingroup$ And still persisting in 13.1 :-( $\endgroup$
    – dnet
    Jul 27, 2022 at 11:17

2 Answers 2



Execute the following code to fix the problem:

DownValues[Parallel`Protected`DistDefs] =
  DownValues[Parallel`Protected`DistDefs] /.
     u : Parallel`Parallel`Private`updates =
     ] :>
     u = Replace[
       defs : {(_HoldPattern -> _) ..} :> With[
         {res = RuleDelayed @@@ Unevaluated@defs},
         res /; True

What does this do?

As mentioned in the comments, the issue is that Language`ExtendedFullDefinition seems to change the return format at 18 down-values. This causes the subsequent manipulations of DistributeDefinitions on the returned Language`DefinitionList[…] expression to leak the evaluation of the definitions (as their r.h.s. are no longer protected by the HoldRest attribute of RuleDelayed).

The code above fixes this by wrapping the call to Language`ExtendedFullDefinition (which happens in Parallel`Protected`DistDefs) with a piece of code that replaces definitions of the form HoldPattern[…]->… with HoldPattern[…]:>…, which prevents the evaluation leak.

  • $\begingroup$ It worked, thanks! I think this is a terrible Mathematica bug, since no "hack" should be necessary to demand the same behaviour from nI=17 to nI=18. Although the code above works, I am worried if it will not have side effects. That is, can i simply insert it in my init.m file and always use it? $\endgroup$
    – Davi
    Dec 29, 2018 at 15:54
  • 2
    $\begingroup$ Yes, stuff like this is really annoying... Regarding potential side effects: As far as I can tell, the fix should be completely safe: The only thing it does is to replace the definitions using Rule with ones using RuleDelayed, which is definitely supported (since it is what happens for less than 18 definitions). It is also a very localized modification, so it should only affect DistributeDefinitions, which is anyway half broken for more than 17 definitions. But in case you do encounter any issues, please let me know $\endgroup$
    – Lukas Lang
    Dec 29, 2018 at 16:00

When dealing with a larger number of definitions my recommendation is to disable the automatic distribution of definitions with

$DistributedContexts = None

and simply redo the definitions on all parallel kernels:

nI = 20;
(NM[#] := Print[#]) & /@ Range[1, nI];

With[{nI = nI}, ParallelEvaluate[(NM[#] := Print[#])& /@ Range[1, nI];]]

It's faster, too.

  • $\begingroup$ Roman, it took me a while to return to this issue. I am now in Mathematica 12.1.1. Perhaps your solution worked just right in a previous version, but in my current version it is not working. For the "nI=20" case, it prints everything. The other solution, from @LukasLang, still works. $\endgroup$
    – Davi
    Nov 13, 2020 at 4:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.