Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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!


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.

share|improve this question

marked as duplicate by Szabolcs, Leonid Shifrin, Jens, Silvia, Jacob Akkerboom Jul 12 '14 at 18:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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. – Szabolcs Jul 12 '14 at 18:00
This question might be relevant. – Leonid Shifrin Jul 12 '14 at 18:15
@Leonid Thanks, it's a duplicate! – Szabolcs Jul 12 '14 at 18:18
Ok, if you say so. Will vote to close then. – Leonid Shifrin Jul 12 '14 at 18:24
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. – Jens Jul 12 '14 at 18:27

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

For the example from the main question,

PreemptProtect@Block[{x}, Pause[5]]
share|improve this answer
Just thinking aloud ... – Szabolcs Jul 12 '14 at 18:09

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