Merge branch 'master' of ssh://git.indexdata.com/home/git/pub/pazpar2
[pazpar2-moved-to-github.git] / src / session.c
index bb0e915..e2c66d3 100644 (file)
@@ -395,17 +395,27 @@ int session_set_watch(struct session *s, int what,
                       session_watchfun fun, void *data,
                       struct http_channel *chan)
 {
+    int ret;
+    session_enter(s);
     if (s->watchlist[what].fun)
-        return -1;
-    s->watchlist[what].fun = fun;
-    s->watchlist[what].data = data;
-    s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
-                                               session_watch_cancel);
+        ret = -1;
+    else
+    {
+        
+        s->watchlist[what].fun = fun;
+        s->watchlist[what].data = data;
+        s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
+                                                   session_watch_cancel);
+        ret = 0;
+    }
+    session_leave(s);
     return 0;
 }
 
 void session_alert_watch(struct session *s, int what)
 {
+    assert(s);
+    session_enter(s);
     if (s->watchlist[what].fun)
     {
         /* our watch is no longer associated with http_channel */
@@ -422,8 +432,11 @@ void session_alert_watch(struct session *s, int what)
         s->watchlist[what].data = 0;
         s->watchlist[what].obs = 0;
 
+        session_leave(s);
         fun(data);
     }
+    else
+        session_leave(s);
 }
 
 //callback for grep_databases
@@ -435,14 +448,25 @@ static void select_targets_callback(void *context, struct session_database *db)
     client_set_session(cl, se);
 }
 
+static void session_remove_clients(struct session *se)
+{
+    struct client *cl = se->clients;
+    while (cl)
+    {
+        struct client *cl_next = client_next_in_session(cl);
+        client_remove_from_session(cl);
+        client_destroy(cl);
+        cl = cl_next;
+    }
+    se->clients = 0;
+}
+
 // Associates a set of clients with a session;
 // Note: Session-databases represent databases with per-session 
 // setting overrides
 static int select_targets(struct session *se, const char *filter)
 {
-    while (se->clients)
-        client_destroy(se->clients);
-
+    session_remove_clients(se);
     return session_grep_databases(se, filter, select_targets_callback);
 }
 
@@ -475,11 +499,11 @@ enum pazpar2_error_code search(struct session *se,
     *addinfo = 0;
     
     session_enter(se);
+    reclist_destroy(se->reclist);
+    se->reclist = 0;
     nmem_reset(se->nmem);
     se->relevance = 0;
     se->total_records = se->total_hits = se->total_merged = 0;
-    reclist_destroy(se->reclist);
-    se->reclist = 0;
     se->num_termlists = 0;
     live_channels = select_targets(se, filter);
     if (!live_channels)
@@ -622,8 +646,8 @@ void destroy_session(struct session *s)
 {
     struct session_database *sdb;
 
-    while (s->clients)
-        client_destroy(s->clients);
+    session_remove_clients(s);
+
     for (sdb = s->databases; sdb; sdb = sdb->next)
         session_database_destroy(sdb);
     normalize_cache_destroy(s->normalize_cache);
@@ -1086,27 +1110,27 @@ static int ingest_to_cluster(struct client *cl,
 int ingest_record(struct client *cl, const char *rec,
                   int record_no, NMEM nmem)
 {
-    struct session_database *sdb = client_get_database(cl);
     struct session *se = client_get_session(cl);
+    int ret;
+    struct session_database *sdb = client_get_database(cl);
     struct conf_service *service = se->service;
     xmlDoc *xdoc = normalize_record(sdb, service, rec, nmem);
     xmlNode *root;
     const char *mergekey_norm;
-    int ret;
-
+    
     if (!xdoc)
         return -1;
-
+    
     root = xmlDocGetRootElement(xdoc);
-
+    
     if (!check_record_filter(root, sdb))
     {
         yaz_log(YLOG_WARN, "Filtered out record no %d from %s", record_no,
-            sdb->database->url);
+                sdb->database->url);
         xmlFreeDoc(xdoc);
         return -1;
     }
-
+    
     mergekey_norm = get_mergekey(xdoc, cl, record_no, service, nmem);
     if (!mergekey_norm)
     {
@@ -1117,9 +1141,8 @@ int ingest_record(struct client *cl, const char *rec,
     session_enter(se);
     ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm);
     session_leave(se);
-
+    
     xmlFreeDoc(xdoc);
-
     return ret;
 }