From d93ce5cde23b7170b96fd9816e8d5e8cfdf6e0f6 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Tue, 21 Jan 2014 11:18:28 -0500
Subject: [PATCH] Fix concurrent connections to IMAP while searching

---
 program/lib/Roundcube/rcube_imap_search.php |   11 +++++------
 1 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/program/lib/Roundcube/rcube_imap_search.php b/program/lib/Roundcube/rcube_imap_search.php
index 70a11bc..c881981 100644
--- a/program/lib/Roundcube/rcube_imap_search.php
+++ b/program/lib/Roundcube/rcube_imap_search.php
@@ -189,6 +189,8 @@
         $imap = $this->worker->get_imap();
 
         if (!$imap->connected()) {
+            trigger_error("No IMAP connection for $this->folder", E_USER_WARNING);
+
             if ($this->threading) {
                 return new rcube_result_thread();
             }
@@ -280,14 +282,13 @@
     public $options;
 
     private $conn;
+    private $counts = 0;
 
     /**
      * Default constructor
      */
     public function __construct($id, $options)
     {
-        $options['ident']['command'] = 'search-'.$id;
-
         $this->id = $id;
         $this->options = $options;
     }
@@ -298,21 +299,19 @@
     public function get_imap()
     {
         // TODO: make this connection persistent for several jobs
-        #if ($this->conn)
-        #    return $this->conn;
+        // This doesn't seem to work. Socket connections don't survive serialization which is used in pthreads
 
         $conn = new rcube_imap_generic();
         # $conn->setDebug(true, function($conn, $message){ trigger_error($message, E_USER_NOTICE); });
 
         if ($this->options['user'] && $this->options['password']) {
-            // TODO: do this synchronized to avoid warnings like "Only one Id allowed in non-authenticated state"
+            $this->options['ident']['command'] = 'search-' . $this->id . 't' . ++$this->counts;
             $conn->connect($this->options['host'], $this->options['user'], $this->options['password'], $this->options);
         }
 
         if ($conn->error)
             trigger_error($conn->error, E_USER_WARNING);
 
-        #$this->conn = $conn;
         return $conn;
     }
 

--
Gitblit v1.9.1