Index: wp-includes/rewrite.php
===================================================================
--- wp-includes/rewrite.php	(revision 30562)
+++ wp-includes/rewrite.php	(working copy)
@@ -2041,6 +2041,12 @@
 	 * @param bool $hard Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard).
 	 */
 	public function flush_rules($hard = true) {
+		// Prevent this action from running before shutdown
+		if ( ! doing_action( 'shutdown' ) ) {
+			add_action( 'shutdown', array( $this, 'flush_rules' ) );
+			return;
+		}
+
 		delete_option('rewrite_rules');
 		$this->wp_rewrite_rules();
 		/**
