2
$\begingroup$

This may have been discussed before, if so, please let me know.


Consider the following example:

x = 5

Dynamic[{Clock[], x}]

This will always display the current value of x.

Now evaluate

Block[{x}, Pause[5]]

Notice that x loses its global value!


Question:

Block is commonly used to temporarily override definitions of user-defined symbols, package symbols, or built-ins. (There's also Interal`InheritedBlock.)

What if, while the main (synchronous) evaluation is in the middle of a Block, it is interrupted by a pre-emptive (asynchronous) evaluation? This pre-emptive evaluation could come from some Dynamic construct in the front end or from a scheduled task.

The Block in the main evaluation could potentially wreak havoc in the (supposedly independent) pre-emptive evaluation. This could cause very difficult to debug timing-dependent bugs that seem to occur completely randomly. This is very likely to happen if Block is used to override a builtin.

$\endgroup$
5
  • 1
    $\begingroup$ I find it quite disappointing that for every advanced programming trick in Mathematica there seems to be a situation where it can cause serious problems. I usually stay away from these types of tricks, but now it looks like even those that I thought to be safe before have their own problems. $\endgroup$
    – Szabolcs
    Jul 12, 2014 at 18:00
  • $\begingroup$ This question might be relevant. $\endgroup$ Jul 12, 2014 at 18:15
  • 1
    $\begingroup$ @Leonid Thanks, it's a duplicate! $\endgroup$
    – Szabolcs
    Jul 12, 2014 at 18:18
  • $\begingroup$ Ok, if you say so. Will vote to close then. $\endgroup$ Jul 12, 2014 at 18:24
  • $\begingroup$ Right, that does happen in version 8, too. It's indeed a duplicate. So this doesn't explain my current v. 10 issues with inputs suddenly becoming "unevaluated" - I haven't even been able to isolate it enough to ask a question about it... very frustrating, it's some Dynamic thing. $\endgroup$
    – Jens
    Jul 12, 2014 at 18:27

1 Answer 1

1
$\begingroup$

One potential solution to this would be using PreemptProtect to prevent interruption.

For the example from the main question,

PreemptProtect@Block[{x}, Pause[5]]
$\endgroup$
1
  • $\begingroup$ Just thinking aloud ... $\endgroup$
    – Szabolcs
    Jul 12, 2014 at 18:09

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