Devel's dpm needs two refreshes to display message from (pre)process I use Zen and have copy/pasted the STARTERKIT to my foo template.
Devel is enabled with the "Rebuild the theme registry on every page load" setting on.

In my completely empty template.php I type in the following and save:

function foo_process_node(&amp;$variables, $hook) {
    dpm('test');
}


I then do drush cc all and refresh page. The message is not shown. If I refresh again the message shows.

If I change test to foobar on the 1st refresh I get the old test and on the second I get foobar.

Why does this happen and how can I avoid it?

Note that this lagging behavior is the same for preprocess and process but not there if I print the message from the foo_page_alter(&amp;$page) function.
This is a common issue people encounter when themeing Drupal (it used to drive me crazy).

In basic terms what's happening is that the messages held in the $_SESSION super global have already been committed to the page by the time the template file containing your call to dpm() is invoked.

This means that the messages are added to the $_SESSION but aren't dumped out to the messages area until the next page...as $messages has already been set up for/printed in page.tpl.php.

The normal way to get around this is to invoke the dpm() at an earlier stage of the process, like in hook_page_alter() as you mention, or hook_node_view(), etc.

Also I think if you implement your preprocess hook at the module level, rather than the theme level, that your calls to dpm() will happen in time for the messages to be displayed in that particular page request.