# Possible bug in Dynamic

The code below crashes the Mathematica kernel in version 10 (not in V9) every time I run it on Windows and on Mac OS. I've sent it to WRI, but was told that they could not reproduce the crash.

Does this really only crash on my computers? Could somebody else test it with Mathematica 10?

pts = Flatten[
Table[Through[{Cos, Sin}[α]] r, {α, 0.,
360 ° - 10^-5, 60 °}, {r, 1, 2}], 1];

LocatorPane[Dynamic[pts],
Dynamic@Show[
Graphics[{Arrow /@ Partition[pts, 2]}], {a, b} =
Transpose[Map[Module[{p1, p2, d}, {p1, p2} = #;
d = p2 - p1;
{d, p1.d}] &, Partition[pts, 2]]];
RegionPlot[And @@ Thread[a.{x, y} < b], {x, -2, 2}, {y, -2, 2}]]]

ContourPlot[
Total[Log[Clip[b - a.{x, y}, {10^-10, ∞}]]], {x, -2,
2}, {y, -2, 2}, PlotRange -> {-10, 0}]

• Don't forget to add the crash tag; in addition to categorization it helps to warn people who blindly execute Question code. :-) – Mr.Wizard Jul 25 '14 at 14:54
• Crash confirmed in a fresh kernel. Version 10.0.0 under Windows. – Mr.Wizard Jul 25 '14 at 14:55
• Crashes for me (Mac OSX), if I put the code in a single cell. If I put the ContourPlot in a separate cell, it does not crash. Reminds me of my experience with this question, which seems to have nothing to do with Dynamic. – Michael E2 Jul 25 '14 at 15:11
• I can confirm the crash, regardless of whether I put the ContourPlot in a separate cell or not. OS X 10.9.4, Mathematica 10.0.0. – Szabolcs Jul 25 '14 at 16:20
• FWIW, Dynamic@Refresh[ContourPlot[...], None] does not crash. – Michael E2 Jul 25 '14 at 17:28

Edit: This is a rewrite. The original answer was more or less an extended comment containing some seemingly relevant clues. I've tracked down two probable issues, each related to a system variable, and there may be others. At least in either case, the crashing can be triggered or prevented.* It turns out that Dynamic@RegionPlot[..] depends on the variables $PlotTheme and $DisplayFunction and that evaluating ContourPlot triggers an update. Beyond this, I'm not sure how the crash occurs. The evaluation of ContourPlot triggers an update of RegionPlot. This apparently occurs simultaneously, the ContourPlot being requested via the main link and the RegionPlot via the preemptive link. It would not be surprising if one interferes with the other and leads to the crash.

The following shows ContourPlot tickles $PlotTheme and $DisplayFunction. Evaluate ContourPlot repeatedly and see pt and df be incremented.

pt = 0; df = 0;
Dynamic[{++pt, $PlotTheme}, TrackedSymbols :> {$PlotTheme}]
Dynamic[{++df, $DisplayFunction}, TrackedSymbols :> {$DisplayFunction}]

(* new cell *)
ContourPlot[x + y, {x, -2, 2}, {y, -2, 2}]


The following shows that Dynamic@RegionPlot depends on $PlotTheme and $DisplayFunction. Evaluate the plot and then evaluate the others in turn and see the RegionPlot be updated.

Dynamic[RegionPlot[x^2 + y^2 < 1, {x, -2, 2}, {y, -2, 2}]]


$DisplayFunction = Hold  $DisplayFunction = Identity
$PlotTheme = "Web"  $PlotTheme = Automatic


Here is a simple workaround: add TrackedSymbols :> {pts}. Below you can uncomment the other symbols, if you wish to observe the kernel crashing. (It seems to be a race situation. It doesn't always crash when you evaluate the ContourPlot, but it usually does. It crashes if you uncomment one, the other, or both symbols.)

LocatorPane[Dynamic[pts],
Dynamic[Show[
Graphics[{Arrow /@ Partition[pts, 2]}], {a1, b1} =
Transpose[Map[Module[{p1, p2, d}, {p1, p2} = #;
d = p2 - p1;
{d, p1.d}] &, Partition[pts, 2]]];
RegionPlot[
And @@ Thread[a1.{x1, y1} < b1], {x1, -2, 2}, {y1, -2, 2}]],
TrackedSymbols :> {pts(*, $PlotTheme*)(*,$DisplayFunction*)}]]


Acknowledgement: Karsten 7.'s observation about SynchronousUpdating -> False led me to observe that evaluating ContourPlot causes the dynamic RegionPlot to be reevaluated. Further investigation led me as far as $PlotTheme and $DisplayFunction.

• +1 Nice troubleshooting. Wrapping the ContourPlot in PreemptProtect seems to work too. – Simon Woods Jul 26 '14 at 18:57
• @SimonWoods Thanks, I'd forgotten about PreemptProtect. It makes sense it would work. – Michael E2 Jul 26 '14 at 19:02
• @SimonWoods I find that, if all code is in one cell, while PreemptProtect@ContourPlot[..] prevents a crash for the first execution of both plots, the next time I execute both plots, things lock up and the dynamic updating of Graphics/RegionPlot does not finish. Also, {a, b} is not initialized when ContourPlot is executed. If the code for the plots is in separate cells, then everything seems to work fine. – Michael E2 Jul 27 '14 at 3:27

The x and y of the ContourPlot are interfering with the x and y inside of Dynamic.
If you use for example

ContourPlot[Total[Log[Clip[b-a.{x2,y2},{10^-10,\[Infinity]}]]],{x2,-2,2},{y2,-2,2},PlotRange->{-10,0}]


it'll not crash.
I guess this shouldn't be necessary.

• It still crashes for me (Mac OSX). – Michael E2 Jul 25 '14 at 15:09
• This works for me. Windows 8.1 – RunnyKine Jul 25 '14 at 15:13
• I'm using v10.0.0 under Win7 – Karsten 7. Jul 25 '14 at 15:15
• Yes, putting the ContourPlot inside Module[{x,y}, ... works, too. (I actually included that workaround in my WRI support ticket, but forgot it in the question here...) – Niki Estner Jul 25 '14 at 15:22
• @MichaelE2: You're right, this looks like a race condition: If I assign all Mathematica processes to a single CPU core, it doesn't crash anymore (or at least not that often). Maybe the WRI support people can't reproduce it because they have single core CPUs? – Niki Estner Jul 25 '14 at 15:56

The more general solution to this problem is to make use of the Option SynchronousUpdating->False:

pts=Flatten[Table[Through[{Cos,Sin}[\[Alpha]]] r,{\[Alpha],0.,360 \[Degree]-10^-5,60 \[Degree]},{r,1,2}],1];
LocatorPane[Dynamic[pts],
Dynamic[Show[
Graphics[{Arrow /@ Partition[pts, 2]}], {a, b} =
Transpose[Map[Module[{p1, p2, d}, {p1, p2} = #;
d = p2 - p1;
{d, p1.d}] &, Partition[pts, 2]]];
RegionPlot[And @@ Thread[a.{x, y} < b], {x, -2, 2}, {y, -2, 2}]],
SynchronousUpdating -> False]]


and than

ContourPlot[Total[Log[Clip[b-a.{x,y},{10^-10,\[Infinity]}]]],{x,-2,2},{y,-2,2},PlotRange->{-10,0}]

• @MichaelE2 does this also work on a Mac? – Karsten 7. Jul 25 '14 at 23:06
• Sorry, somehow I wasn't pinged. It does work (at least for the several times I tried it). Of course, a, b are not set until the RegionPlot is updated. Also executing ContourPlot[Total[Log[Clip[b2 - a2.{x2, y2}, {10^-10, \[Infinity]}]]], {x2, -2, 2}, {y2, -2, 2}, PlotRange -> {-10, 0}] causes the RegionPlot to update. (The variables a2, b2 are copies of a, b, resp.) – Michael E2 Jul 26 '14 at 2:35

This, I think, is a simplified version of the issue:
If one executes

Dynamic[Print[x]; x]


and than with the Messages window open

Plot[x,{x,-2,2}]


x is printet into the Messages window two times whenever the prior Plot comment is executed (using Mma v10).
However, in Mma v9 only one x is printed into the Messages window for every execution of the Plot command.

• Please note that on my system if instead of x, y, I use RegionPlot[And @@ Thread[a.{x1, y1} < b], {x1, -2, 2}, {y1, -2, 2}] and ContourPlot[Total[Log[Clip[b - a.{x2, y2}, {10^-10, \[Infinity]}]]], {x2, -2, 2}, {y2, -2, 2}, PlotRange -> {-10, 0}] causes a crash. So at least for this example on a Mac, there's something beyond a symbol clash. – Michael E2 Jul 25 '14 at 16:45
• @MichaelE2 what if you also lokalize a and b for the ContourPlot? – Karsten 7. Jul 25 '14 at 16:58
• I tried that. And localizing a, b, x and y. They all crash. – Michael E2 Jul 25 '14 at 17:22