diff --git a/src/OCE/SBC/cpl_oasis3.F90 b/src/OCE/SBC/cpl_oasis3.F90
index 0147ffdac802e5234499df6ae87b268afefcab8b..111343f970676e1324dc282eda037b93c202c8de 100644
--- a/src/OCE/SBC/cpl_oasis3.F90
+++ b/src/OCE/SBC/cpl_oasis3.F90
@@ -25,6 +25,9 @@ MODULE cpl_oasis3
    !!----------------------------------------------------------------------
 #if defined key_oasis3
    USE mod_oasis                    ! OASIS3-MCT module
+#endif
+#if defined key_xios
+   USE xios                         ! I/O server
 #endif
    USE par_oce                      ! ocean parameters
    USE dom_oce                      ! ocean space and time domain
@@ -137,8 +140,9 @@ CONTAINS
       !
       INTEGER :: id_part
       INTEGER :: paral(5)       ! OASIS3 box partition
-      INTEGER :: ishape(4)    ! shape of arrays passed to PSMILe
+      INTEGER :: ishape(4)      ! shape of arrays passed to PSMILe
       INTEGER :: ji,jc,jm       ! local loop indicees
+      LOGICAL :: llenddef       ! should we call xios_oasis_enddef and oasis_enddef?
       CHARACTER(LEN=64) :: zclname
       CHARACTER(LEN=2) :: cli2
       !!--------------------------------------------------------------------
@@ -292,14 +296,21 @@ CONTAINS
       !------------------------------------------------------------------
       !
 #if defined key_agrif
-      ! Warning: Agrif_Nb_Fine_Grids not yet defined at this stage for Agrif_Root -> must use Agrif_Root_Only()
-      IF( Agrif_Root_Only() .OR. agrif_fixed() == Agrif_Nb_Fine_Grids() ) THEN
-#endif
-      CALL oasis_enddef(nerror)
-      IF( nerror /= OASIS_Ok )   CALL oasis_abort ( ncomp_id, 'cpl_define', 'Failure in oasis_enddef')
-#if defined key_agrif
+      IF( Agrif_Root() ) THEN   ! Warning: Agrif_Nb_Fine_Grids not yet defined -> must use Agrif_Root_Only()
+         llenddef = Agrif_Root_Only()   ! true of no nested grid
+      ELSE                      ! Is it the last nested grid?
+         llenddef = agrif_fixed() == Agrif_Nb_Fine_Grids()
       ENDIF
+#else
+      llenddef = .TRUE.
+#endif
+      IF( llenddef ) THEN
+#if defined key_xios
+         CALL xios_oasis_enddef()   ! see "Joint_usage_OASIS3-MCT_XIOS.pdf" on XIOS wiki page
 #endif
+         CALL oasis_enddef(nerror)
+         IF( nerror /= OASIS_Ok )   CALL oasis_abort ( ncomp_id, 'cpl_define', 'Failure in oasis_enddef')
+      ENDIF
       !
    END SUBROUTINE cpl_define
 
diff --git a/tests/CPL_OASIS/EXPREF/iodef.xml b/tests/CPL_OASIS/EXPREF/iodef.xml
index dda919bd7ac24d1d0e892e5bd6e3cb17ca357be4..a76b6d8115cff47796e6273ebfdff7a6ae4b0a79 100644
--- a/tests/CPL_OASIS/EXPREF/iodef.xml
+++ b/tests/CPL_OASIS/EXPREF/iodef.xml
@@ -13,7 +13,7 @@
           <variable id="using_server"              type="bool">true</variable>
           <variable id="using_oasis"               type="bool">true</variable>
           <variable id="oasis_codes_id"            type="string" >oceanx</variable>
-          <variable id="call_oasis_enddef"         type="bool">false</variable>
+          <variable id="call_oasis_enddef"         type="bool">true</variable>
 
       </variable_definition>
   </context>