drupal_set_message doesn't display message sometimes I'm uploading a file, to process and display a message at the end. However, the messages that drupal_set_message is set to display are displayed sometimes and don't display sometimes, even, if the processing runs successfully in the background. The reload time restriction isn't a problem as it is set to multiple minutes and the processing hardly takes 10-60 seconds on average. Is there a way to force the page to show the messages at any cost? The drupal_set_messages that I am concerned about are the last two drupal_set_messages in the code. I've even tried using drupal_set_message(t('!shell_c', array('!shell_c'=&gt;$shell_command)), 'status'); but the issue persists where the drupal_set_message messages are shown sometimes and not shown sometimes, rather erratically.
//Before this part of the code, there is function to create Menu ($items[])
function g_p_proc_form($form, &amp;$form_state, $values) {
    $form['#attributes']['enctype'] = 'multipart/form-data';
    $form['filter'] = array (
        &quot;#type&quot; =&gt; &quot;fieldset&quot;,
        &quot;#title&quot; =&gt; t(&quot;File Process &lt;BR&gt; &quot;),
        &quot;#collapsible&quot; =&gt; TRUE,
    );
  // without this attribute, upload may fail on submit
    $form['#attributes']['enctype'] = 'multipart/form-data';
    $form['filter']['file'] = array(
        // '#title' =&gt; t('File processing'),
        '#type'  =&gt; 'file',
        '#name' =&gt; 'files[]',
        '#description' =&gt; t('Select Submit to start processing the file'),
        '#type'  =&gt; 'file'),
        '#upload_location' =&gt; 'public://',
        '#attributes' =&gt; array('multiple' =&gt; 'multiple'),
    );
    // echo '&lt;script type=&quot;text/javascript&quot;&gt;Drupal.behaviors.DSU_batch_report;&lt;/script&gt;'; //christ
    if (isset($_SERVER['CONTENT_LENGTH']) &amp;&amp; (int) $_SERVER['CONTENT_LENGTH'] &gt; 'size based on my requirements') {
        drupal_set_message(t(&quot; Warning message on file size&quot;));));
        // unset($_SERVER['CONTENT_LENGTH']);
    }


    $form['filter']['submit_upload'] = array(
        '#type'  =&gt;  'submit', 
        '#value' =&gt;  t('Submit'),

    );


    // drupal_set_message(t(&quot;in fn&quot;));

    return $form;
}

function g_p_proc_form_submit($form, &amp;$form_state) {
    header(current page)
    try{
        $number_file = count($_FILES['files']['name']);
        $f_name = array();
        for ($i=0; $i&lt;$number_file;$i++)
        {
            $file = file_save_upload($i, array('file_validate_extensions' =&gt; array('pdf PDF')), 'public://', FILE_EXISTS_REPLACE);
            $f_name[] = $_FILES['files']['name'][$i]; //[]
        }
            }catch (Exception $error){
        echo 'Exception: ',  $error-&gt;getMessage(), &quot;\n&quot;;
    }
    $n_f = count($f_name);
    drupal_set_message(t('Number of files to process: !n_f', array('!number_f'=&gt;$n_f)));
    $fn_name = 'g_p';
    $fn_call = $fn_name($f_name);
function g_p($f_name=array()){
    $curr_l = getcwd();
    chdir('My file processing PHP script location ');
    $n_f = count($f_name);
    $shell_command = shell_exec(&quot;file_processing_php_script_name.php&quot;);
    //header(&quot;Location: &quot;Current file processing module menu site&quot;); //Commented out
    drupal_set_message($shell_command); //print_r statements from the file_processing_php_script_name.php
    chdir($curr_l);
}
It sounds like you need to maintain a stable message storage solution until the operation is complete.
The D7 core message functions drupal_set_message() &amp; drupal_get_messages() (the core function used when retrieving messages for rendering) are pushing/popping messages to session var, $_SESSION['messages'] These messages are designed around a single page request/received operation.
If there many different async requests going on with the same session (or other modules calling drupal_get_messages() with its default options), its likely $_SESSION['messages'] is being cleared out before rendering when you need it.
Try storing your messages somewhere else (e.g. another session variable) then call drupal_set_message() with your messages once the full operation is complete. If most messages are background ops, you should use consider watchdog() for logging background ops and then use drupal_set_message() for a &quot;operation complete&quot; message.