Index: /trunk/roundcubemail/CHANGELOG
===================================================================
--- /trunk/roundcubemail/CHANGELOG	(revision 5225)
+++ /trunk/roundcubemail/CHANGELOG	(revision 5226)
@@ -2,4 +2,5 @@
 ===========================
 
+- Fix session race conditions when composing new messages
 - Fix encoding of LDAP contacts identifiers (#1488079)
 - jQuery 1.6.4
Index: /trunk/roundcubemail/program/include/rcube_json_output.php
===================================================================
--- /trunk/roundcubemail/program/include/rcube_json_output.php	(revision 5225)
+++ /trunk/roundcubemail/program/include/rcube_json_output.php	(revision 5226)
@@ -220,4 +220,5 @@
     public function send()
     {
+      sleep(6);
         $this->remote_response();
         exit;
Index: /trunk/roundcubemail/program/include/rcube_session.php
===================================================================
--- /trunk/roundcubemail/program/include/rcube_session.php	(revision 5225)
+++ /trunk/roundcubemail/program/include/rcube_session.php	(revision 5226)
@@ -322,7 +322,7 @@
   public function cleanup()
   {
-    // current compose information is stored in $_SESSION['compose'], move it to $_SESSION['compose_data']
-    if ($_SESSION['compose']) {
-      $_SESSION['compose_data'][$_SESSION['compose']['id']] = $_SESSION['compose'];
+    // current compose information is stored in $_SESSION['compose'], move it to $_SESSION['compose_data_<ID>']
+    if ($compose_id = $_SESSION['compose']['id']) {
+      $_SESSION['compose_data_'.$compose_id] = $_SESSION['compose'];
       $this->remove('compose');
     }
Index: /trunk/roundcubemail/program/steps/mail/attachments.inc
===================================================================
--- /trunk/roundcubemail/program/steps/mail/attachments.inc	(revision 5225)
+++ /trunk/roundcubemail/program/steps/mail/attachments.inc	(revision 5226)
@@ -26,5 +26,5 @@
 
 $COMPOSE_ID = get_input_value('_id', RCUBE_INPUT_GPC);
-$_SESSION['compose'] = $_SESSION['compose_data'][$COMPOSE_ID];
+$_SESSION['compose'] = $_SESSION['compose_data_'.$COMPOSE_ID];
 
 if (!$_SESSION['compose']) {
Index: /trunk/roundcubemail/program/steps/mail/compose.inc
===================================================================
--- /trunk/roundcubemail/program/steps/mail/compose.inc	(revision 5225)
+++ /trunk/roundcubemail/program/steps/mail/compose.inc	(revision 5226)
@@ -30,5 +30,5 @@
 
 $COMPOSE_ID = get_input_value('_id', RCUBE_INPUT_GET);
-$_SESSION['compose'] = $_SESSION['compose_data'][$COMPOSE_ID];
+$_SESSION['compose'] = $_SESSION['compose_data_'.$COMPOSE_ID];
 
 // Nothing below is called during message composition, only at "new/forward/reply/draft" initialization or
@@ -40,5 +40,5 @@
     raise_error(array('code' => 500, 'type' => 'php',
       'file' => __FILE__, 'line' => __LINE__,
-      'message' => "Invalid session"), true, true);
+      'message' => "Invalid compose ID"), true, true);
 
   $_SESSION['compose'] = array(
Index: /trunk/roundcubemail/program/steps/mail/func.inc
===================================================================
--- /trunk/roundcubemail/program/steps/mail/func.inc	(revision 5225)
+++ /trunk/roundcubemail/program/steps/mail/func.inc	(revision 5226)
@@ -1431,10 +1431,10 @@
 function rcmail_compose_cleanup($id)
 {
-  if (!isset($_SESSION['compose_data'][$id]))
+  if (!isset($_SESSION['compose_data_'.$id]))
     return;
 
   $rcmail = rcmail::get_instance();
   $rcmail->plugins->exec_hook('attachments_cleanup', array('group' => $id));
-  unset($_SESSION['compose_data'][$id]);
+  $rcmail->session->remove('compose_data_'.$id);
 }
 
Index: /trunk/roundcubemail/program/steps/mail/sendmail.inc
===================================================================
--- /trunk/roundcubemail/program/steps/mail/sendmail.inc	(revision 5225)
+++ /trunk/roundcubemail/program/steps/mail/sendmail.inc	(revision 5226)
@@ -28,5 +28,5 @@
 
 $COMPOSE_ID = get_input_value('_id', RCUBE_INPUT_GPC);
-$_SESSION['compose'] = $_SESSION['compose_data'][$COMPOSE_ID];
+$_SESSION['compose'] = $_SESSION['compose_data_'.$COMPOSE_ID];
 
 /****** checks ********/
