[wp-trac] [WordPress Trac] #15344: global variables get cleared

WordPress Trac wp-trac at lists.automattic.com
Tue Nov 9 12:54:09 UTC 2010


#15344: global variables get cleared
-------------------------------+--------------------------------------------
 Reporter:  OS1                |        Owner:                 
     Type:  defect (bug)       |       Status:  closed         
 Priority:  normal             |    Milestone:  Awaiting Review
Component:  General            |      Version:  3.0.1          
 Severity:  normal             |   Resolution:  invalid        
 Keywords:  reporter-feedback  |  
-------------------------------+--------------------------------------------

Comment(by OS1):

 Ok, so here's the scenario as best as I can determine, and I don't fully
 understand what is happening. I could be being mislead by the tools and
 debug output.

 Change index.php to be
 if (!isset($wp_did_header))
 {
   $wp_did_header = true;
 }
 and that's all.

 Now add debug code or set a breakpoint at the first line. Refresh the page
 in a browser. The script is called twice. Odd seeing as how this is the
 server entry point. Second time around wp_did_header is cleared as are all
 globals because this is now a new instance.

 Step 2, rename the project base directory, i.e. effectively remove WP.
 Make a new project base directory and copy in the modified index.php.
 Refresh the browser page and now, as expected, the code is only called
 once.

 So put everything back as it should be but now add the debug code or set
 the break point in the plugin init function. This is now called three
 times and the stack trace indicates that index.php has been re-entered
 each time thus instantiating a new set of globals.

 The result of this appears unexpected. On a "registration failure" the
 error messages make it out the the browser, but for other scenarios the
 messages are lost due to what? re-entrancy?! It looks for all the world
 like a race condition! Which is why I can't get my head around exactly
 what is happening.

 Using this code
 $handle = fopen("log.txt", "r");
 $res = fread($handle, 2);
 fclose($handle);
 $handle = fopen("log.txt", "w");
 $str = $res + 1;
 fwrite($handle, $str);
 fclose($handle);

 making sure that log.txt contains 0 and pressing the refresh button of the
 page only once results in the file contents being 3, suggesting that the
 thread of execution has been entered three times.

 Putting similar code where I populate the global variable $shopMesssages
 results in a file containing the expected error messages even though the
 global variable has been cleared.

 I can't even populate session variables since these are also cleared as
 this is a new session!

 I'll look into WP_Error but surely this will suffer from the same effect.
 I probably need to seriously re-organise my code to try to get around this
 race condition :-(

 Have I lost the plot?! Is this just the way we use WP? Have I missed
 something obvious? Am I just being stoooopid?!

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/15344#comment:7>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list