Index: /XIOS/dev/dev_olga/bld.cfg
===================================================================
--- /XIOS/dev/dev_olga/bld.cfg	(revision 1138)
+++ /XIOS/dev/dev_olga/bld.cfg	(revision 1139)
@@ -34,11 +34,11 @@
 bld::target xios_server.exe 
 bld::target test_regular.exe
-#bld::target test_xios2_cmip6.exe
+bld::target test_xios2_cmip6.exe
 #bld::target test_new_features.exe test_unstruct_complete.exe 
 bld::target test_remap.exe
 bld::target test_complete.exe
 bld::target test_client.exe
-bld::target test_unstruct_complete.exe
-bld::target test_unstructured.exe
+#bld::target test_unstruct_complete.exe
+#bld::target test_unstructured.exe
 bld::exe_dep
 
Index: /XIOS/dev/dev_olga/src/context_client.cpp
===================================================================
--- /XIOS/dev/dev_olga/src/context_client.cpp	(revision 1138)
+++ /XIOS/dev/dev_olga/src/context_client.cpp	(revision 1139)
@@ -276,5 +276,7 @@
       map<int,CClientBuffer*>::iterator itBuff;
       for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++)
+      {
           delete itBuff->second;
+      }
       buffers.clear();
    }
@@ -420,4 +422,44 @@
     //releaseBuffers(); // moved to CContext::finalize()
   }
+
+  /*!
+  * Finalize context client and do some reports. Function is non-blocking.
+  */
+ void CContextClient::postFinalize(void)
+ {
+   map<int,CClientBuffer*>::iterator itBuff;
+   bool stop = false;
+
+   CTimer::get("Blocking time").resume();
+   while (hasTemporarilyBufferedEvent())
+   {
+     checkBuffers();
+     sendTemporarilyBufferedEvent();
+   }
+   CTimer::get("Blocking time").suspend();
+
+   CEventClient event(CContext::GetType(), CContext::EVENT_ID_CONTEXT_POST_FINALIZE);
+   if (isServerLeader())
+   {
+     CMessage msg;
+     const std::list<int>& ranks = getRanksServerLeader();
+     for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
+       event.push(*itRank, 1, msg);
+     sendEvent(event);
+   }
+   else sendEvent(event);
+
+   CTimer::get("Blocking time").resume();
+//    while (!stop)
+   {
+     checkBuffers();
+     if (hasTemporarilyBufferedEvent())
+       sendTemporarilyBufferedEvent();
+
+     stop = true;
+   }
+   CTimer::get("Blocking time").suspend();
+
+ }
 
   /*!
Index: /XIOS/dev/dev_olga/src/context_client.hpp
===================================================================
--- /XIOS/dev/dev_olga/src/context_client.hpp	(revision 1138)
+++ /XIOS/dev/dev_olga/src/context_client.hpp	(revision 1139)
@@ -53,4 +53,5 @@
 //      void closeContext(void);  Never been implemented.
       void finalize(void);
+      void postFinalize(void);
 
       void setBufferSize(const std::map<int,StdSize>& mapSize, const std::map<int,StdSize>& maxEventSize);
Index: /XIOS/dev/dev_olga/src/context_server.cpp
===================================================================
--- /XIOS/dev/dev_olga/src/context_server.cpp	(revision 1138)
+++ /XIOS/dev/dev_olga/src/context_server.cpp	(revision 1139)
@@ -225,5 +225,4 @@
     {
       finished=true;
-//      info(20)<<"Server Side context <"<<context->getId()<<"> finalized"<<endl;   // moved to CContext::finalize()
       std::map<int, StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
                            iteMap = mapBufferSize_.end(), itMap;
@@ -237,4 +236,9 @@
       context->finalize();
       report(0)<< " Memory report : Context <"<<ctxId<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl;
+    }
+    else if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_POST_FINALIZE)
+    {
+      info(20)<<"Server side context <"<<context->getId()<<"> finalized."<<endl;
+      context->postFinalize();
     }
     else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event);
Index: /XIOS/dev/dev_olga/src/node/context.cpp
===================================================================
--- /XIOS/dev/dev_olga/src/node/context.cpp	(revision 1138)
+++ /XIOS/dev/dev_olga/src/node/context.cpp	(revision 1139)
@@ -26,5 +26,5 @@
       : CObjectTemplate<CContext>(), CContextAttributes()
       , calendar(), hasClient(false), hasServer(false)
-      , isPostProcessed(false)//, finalized(false)
+      , isPostProcessed(false), finalized(false)
       , idServer_(), client(0), server(0)
       , allProcessed(false), countChildCtx_(0)
@@ -34,5 +34,5 @@
       : CObjectTemplate<CContext>(id), CContextAttributes()
       , calendar(), hasClient(false), hasServer(false)
-      , isPostProcessed(false)//, finalized(false)
+      , isPostProcessed(false), finalized(false)
       , idServer_(), client(0), server(0)
       , allProcessed(false), countChildCtx_(0)
@@ -45,4 +45,5 @@
      for (std::vector<CContextClient*>::iterator it = clientPrimServer.begin(); it != clientPrimServer.end(); it++)  delete *it;
      for (std::vector<CContextServer*>::iterator it = serverPrimServer.begin(); it != serverPrimServer.end(); it++)  delete *it;
+
    }
 
@@ -391,11 +392,16 @@
     else if (CServer::serverLevel == 1)
     {
-      client->checkBuffers();
-      bool serverFinished = server->eventLoop();
+      if (!finalized)
+        client->checkBuffers();
+      bool serverFinished = true;
+      if (!finalized)
+        serverFinished = server->eventLoop();
       bool serverPrimFinished = true;
       for (int i = 0; i < clientPrimServer.size(); ++i)
       {
-        clientPrimServer[i]->checkBuffers();
-        serverPrimFinished *= serverPrimServer[i]->eventLoop();
+        if (!finalized)
+          clientPrimServer[i]->checkBuffers();
+        if (!finalized)
+          serverPrimFinished *= serverPrimServer[i]->eventLoop();
       }
       return ( serverFinished && serverPrimFinished);
@@ -419,4 +425,5 @@
      // (1) blocking send context finalize to its server
      // (2) blocking receive context finalize from its server
+     // (3) send post finalize to its server
      if (CXios::isClient)
      {
@@ -432,4 +439,10 @@
          while (!server->hasFinished())
            server->eventLoop();
+
+         if (CXios::isServer) // Mode attache
+           postFinalize();
+         else                  // Mode server
+           client->postFinalize();
+
        }
      }
@@ -442,5 +455,6 @@
            clientPrimServer[i]->finalize();
 
-       // (Last) context finalized message received => send context finalize to its parent context
+       // (Last) context finalized message received
+       // Close files, gather registries, send context finalize to its parent context
        if (countChildCtx_ == clientPrimServer.size())
          client->finalize();
@@ -448,17 +462,29 @@
        ++countChildCtx_;
      }
-
-     // If in mode attache call postFinalize
-     if (CXios::isServer && CXios::isClient)
-       postFinalize();
    }
 
    /*!
    * \fn void CContext::postFinalize(void)
-   * Close files, gather registries, , and make deallocations.
+   * Close files, gather registries, and make deallocations.
    * Function is called when a context is finalized (it has nothing to receive and nothing to send).
    */
    void CContext::postFinalize(void)
    {
+     finalized = true;
+
+     // Primary server: blocking send post finalize to secondary servers
+     for (int i = 0; i < clientPrimServer.size(); ++i)
+       clientPrimServer[i]->postFinalize();
+     bool buffersReleased;
+     do
+     {
+       buffersReleased = true;
+       for (int i = 0; i < clientPrimServer.size(); ++i)
+       {
+         clientPrimServer[i]->checkBuffers();
+         buffersReleased *= !clientPrimServer[i]->havePendingRequests();
+       }
+     } while (!buffersReleased);
+
      info(20)<<"Context <"<<getId()<<"> is finalized."<<endl;
 
@@ -469,4 +495,9 @@
        if (server->intraCommRank==0) CXios::globalRegistry->mergeRegistry(*registryOut) ;
      }
+
+     //! Deallocate client buffers
+     client->releaseBuffers();
+     for (int i = 0; i < clientPrimServer.size(); ++i)
+       clientPrimServer[i]->releaseBuffers();
 
      //! Free internally allocated communicators
@@ -475,8 +506,4 @@
      comms.clear();
 
-     //! Deallocate client buffers
-     client->releaseBuffers();
-     for (int i = 0; i < clientPrimServer.size(); ++i)
-       clientPrimServer[i]->releaseBuffers();
    }
 
@@ -1000,10 +1027,4 @@
    void CContext::recvCreateFileHeader(CBufferIn& buffer)
    {
-     // The creation of header file should be delegated to server2, for now
-      // if (hasClient && hasServer)
-      // {        
-      //   sendCreateFileHeader();
-      // }
-      
       if (!hasClient && hasServer) 
         createFileHeader();
@@ -1014,5 +1035,4 @@
    {
       // Use correct context client to send message
-     // int nbSrvPools = (hasServer) ? clientPrimServer.size() : 1;
      int nbSrvPools = (this->hasServer) ? (this->hasClient ? this->clientPrimServer.size() : 0) : 1;
      for (int i = 0; i < nbSrvPools; ++i)
@@ -1151,5 +1171,4 @@
       // Find all enabled fields of each file
       this->findAllEnabledFields();
-      // this->findAllEnabledFieldsInReadModeFiles();
       // For now, only read files with client and only one level server
       if (hasClient && !hasServer) this->findAllEnabledFieldsInReadModeFiles();
@@ -1162,23 +1181,9 @@
      }
 
-      // // Only search and rebuild all reference objects of enable fields, don't transform
-      // this->solveOnlyRefOfEnabledFields(false);
-
-      // // Search and rebuild all reference object of enabled fields
-      // this->solveAllRefOfEnabledFields(false);
-
-      // // Find all fields with read access from the public API
-      // findFieldsWithReadAccess();
-      // // and solve the all reference for them
-      // solveAllRefOfFieldsWithReadAccess();
-
       // Only search and rebuild all reference objects of enable fields, don't transform
       this->solveOnlyRefOfEnabledFields(false);
 
       // Search and rebuild all reference object of enabled fields, and transform
-      // this->solveAllEnabledFields
       this->solveAllRefOfEnabledFieldsAndTransform(false);
-      // // Check grid and calculate its distribution
-      // if (hasClient) checkGridEnabledFields();
 
       // Find all fields with read access from the public API
@@ -1585,19 +1590,9 @@
   /*!
   * \fn bool CContext::isFinalized(void)
-  * Context is finalized if:
-  * (1) it has received all context finalize events
-  * (2) it has no pending events to send.
+  * Context is finalized if it received context post finalize event.
   */
   bool CContext::isFinalized(void)
   {
-    if (countChildCtx_==clientPrimServer.size()+1)
-    {
-      bool buffersReleased = !client->havePendingRequests();
-      for (int i = 0; i < clientPrimServer.size(); ++i)
-        buffersReleased *= !clientPrimServer[i]->havePendingRequests();
-      return buffersReleased;
-    }
-    else
-      return false;
+    return finalized;
   }
 
Index: /XIOS/dev/dev_olga/src/node/context.hpp
===================================================================
--- /XIOS/dev/dev_olga/src/node/context.hpp	(revision 1138)
+++ /XIOS/dev/dev_olga/src/node/context.hpp	(revision 1139)
@@ -51,4 +51,5 @@
            EVENT_ID_CLOSE_DEFINITION,EVENT_ID_UPDATE_CALENDAR,
            EVENT_ID_CREATE_FILE_HEADER,EVENT_ID_CONTEXT_FINALIZE,
+           EVENT_ID_CONTEXT_POST_FINALIZE,
            EVENT_ID_POST_PROCESS, EVENT_ID_SEND_REGISTRY,
            EVENT_ID_POST_PROCESS_GLOBAL_ATTRIBUTES,
@@ -247,5 +248,5 @@
          bool isPostProcessed;
          bool allProcessed;
-//         bool finalized;
+         bool finalized;
          int countChildCtx_;        //!< Counter of child contexts (for now it is the number of secondary server pools)
          StdString idServer_;
Index: /XIOS/dev/dev_olga/src/server.cpp
===================================================================
--- /XIOS/dev/dev_olga/src/server.cpp	(revision 1138)
+++ /XIOS/dev/dev_olga/src/server.cpp	(revision 1139)
@@ -319,5 +319,4 @@
        while(!stop)
        {
-
          if (isRoot)
          {
@@ -588,5 +587,4 @@
      {
        bool isFinalized ;
-
        map<string,CContext*>::iterator it ;
 
@@ -596,12 +594,10 @@
          if (isFinalized)
          {
-           it->second->postFinalize();
+//           it->second->postFinalize();
            contextList.erase(it) ;
            break ;
          }
          else
-         {
-           isFinalized=it->second->checkBuffersAndListen();
-         }
+           it->second->checkBuffersAndListen();
        }
      }
