session: cleanup listener segment manager on wrk free

Type: fix

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: I1585af9e4f4273f055aed824b133ddc8eddd1df7
diff --git a/src/vnet/session/application_worker.c b/src/vnet/session/application_worker.c
index 8eebaf0..8e3ba35 100644
--- a/src/vnet/session/application_worker.c
+++ b/src/vnet/session/application_worker.c
@@ -56,7 +56,7 @@
 {
   application_t *app = application_get (app_wrk->app_index);
   vnet_unlisten_args_t _a, *a = &_a;
-  u64 handle, *handles = 0;
+  u64 handle, *handles = 0, *sm_indices = 0;
   segment_manager_t *sm;
   session_t *ls;
   u32 sm_index;
@@ -70,22 +70,29 @@
   hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
     ls = listen_session_get_from_handle (handle);
     vec_add1 (handles, app_listen_session_handle (ls));
+    vec_add1 (sm_indices, sm_index);
     sm = segment_manager_get (sm_index);
-    sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
   }));
   /* *INDENT-ON* */
 
-  hash_free (app_wrk->listeners_table);
-
   for (i = 0; i < vec_len (handles); i++)
     {
+      /* Cleanup listener */
       a->app_index = app->app_index;
       a->wrk_map_index = app_wrk->wrk_map_index;
       a->handle = handles[i];
-      /* seg manager is removed when unbind completes */
       (void) vnet_unlisten (a);
+
+      sm = segment_manager_get_if_valid (sm_indices[i]);
+      if (sm && !segment_manager_app_detached (sm))
+	{
+	  sm->first_is_protected = 0;
+	  segment_manager_init_free (sm);
+	}
     }
   vec_reset_length (handles);
+  vec_free (sm_indices);
+  hash_free (app_wrk->listeners_table);
 
   /*
    * Connects segment manager cleanup
@@ -124,11 +131,11 @@
   vec_free (app_wrk->half_open_table);
   vec_free (handles);
 
-  /* If first segment manager is used by a listener */
-  if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
-      && app_wrk->first_segment_manager != app_wrk->connects_seg_manager)
+  /* If first segment manager is used by a listener that recently
+   * stopped listening, mark it as detached */
+  if (app_wrk->first_segment_manager != app_wrk->connects_seg_manager
+      && (sm = segment_manager_get_if_valid (app_wrk->first_segment_manager)))
     {
-      sm = segment_manager_get (app_wrk->first_segment_manager);
       sm->first_is_protected = 0;
       sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
       /* .. and has no fifos, e.g. it might be used for redirected sessions,
diff --git a/src/vnet/session/segment_manager.c b/src/vnet/session/segment_manager.c
index 3e0b896..33ce6e5 100644
--- a/src/vnet/session/segment_manager.c
+++ b/src/vnet/session/segment_manager.c
@@ -64,7 +64,7 @@
   return props;
 }
 
-static u8
+u8
 segment_manager_app_detached (segment_manager_t * sm)
 {
   return (sm->app_wrk_index == SEGMENT_MANAGER_INVALID_APP_INDEX);
diff --git a/src/vnet/session/segment_manager.h b/src/vnet/session/segment_manager.h
index 13b1fff..fbd9afa 100644
--- a/src/vnet/session/segment_manager.h
+++ b/src/vnet/session/segment_manager.h
@@ -140,6 +140,7 @@
 svm_msg_q_t *segment_manager_event_queue (segment_manager_t * sm);
 u32 segment_manager_evt_q_expected_size (u32 q_size);
 
+u8 segment_manager_app_detached (segment_manager_t * sm);
 void segment_manager_app_detach (segment_manager_t * sm);
 
 /**