Index: NEMO/releases/r4.0/r4.0-HEAD/src/OCE/step.F90
===================================================================
--- NEMO/releases/r4.0/r4.0-HEAD/src/OCE/step.F90	(revision 12640)
+++ NEMO/releases/r4.0/r4.0-HEAD/src/OCE/step.F90	(revision 12651)
@@ -81,4 +81,5 @@
       !! ---------------------------------------------------------------------
 #if defined key_agrif
+      IF( nstop > 0 ) return   ! avoid to go further if an error was detected during previous time step 
       kstp = nit000 + Agrif_Nb_Step()
       IF( lk_agrif_debug ) THEN
@@ -280,12 +281,8 @@
 #if defined key_agrif
       !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-      ! AGRIF
+      ! AGRIF recursive integration
       !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<      
                          CALL Agrif_Integrate_ChildGrids( stp )  ! allows to finish all the Child Grids before updating
-
-                         IF( Agrif_NbStepint() == 0 ) CALL Agrif_update_all( ) ! Update all components
-#endif
-      IF( ln_diaobs  )   CALL dia_obs      ( kstp )      ! obs-minus-model (assimilation) diagnostics (call after dynamics update)
-
+#endif
       !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
       ! Control
@@ -293,4 +290,16 @@
                          CALL stp_ctl      ( kstp, indic )
                          
+#if defined key_agrif
+      !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+      ! AGRIF update
+      !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<      
+      IF( Agrif_NbStepint() == 0 .AND. nstop == 0 ) CALL Agrif_update_all( ) ! Update all components
+#endif
+
+      IF( ln_diaobs  )   CALL dia_obs      ( kstp )      ! obs-minus-model (assimilation) diagnostics (call after dynamics update)
+
+      !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+      ! File manipulation at the end of the first time step
+      !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<                         
       IF( kstp == nit000 ) THEN                          ! 1st time step only
                                         CALL iom_close( numror )   ! close input  ocean restart file
@@ -306,7 +315,10 @@
       !
 #if defined key_iomput
+      !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+      ! Finalize contextes if end of simulation or error detected
+      !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<                         
       IF( kstp == nitend .OR. indic < 0 ) THEN 
                       CALL iom_context_finalize(      cxios_context          ) ! needed for XIOS+AGRIF
-                      IF(lrxios) CALL iom_context_finalize(      crxios_context          )
+         IF( lrxios ) CALL iom_context_finalize(      crxios_context         )
          IF( ln_crs ) CALL iom_context_finalize( trim(cxios_context)//"_crs" ) ! 
       ENDIF
Index: NEMO/releases/r4.0/r4.0-HEAD/src/SAS/step.F90
===================================================================
--- NEMO/releases/r4.0/r4.0-HEAD/src/SAS/step.F90	(revision 12640)
+++ NEMO/releases/r4.0/r4.0-HEAD/src/SAS/step.F90	(revision 12651)
@@ -74,4 +74,5 @@
 
 #if defined key_agrif
+      IF( nstop > 0 ) return   ! avoid to go further if an error was detected during previous time step 
       kstp = nit000 + Agrif_Nb_Step()
       IF ( lk_agrif_debug ) THEN
@@ -104,15 +105,8 @@
 #if defined key_agrif
       !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-      ! AGRIF
+      ! AGRIF recursive integration
       !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<      
                              CALL Agrif_Integrate_ChildGrids( stp )  
-
-      IF( Agrif_NbStepint() == 0 ) THEN               ! AGRIF Update from zoom N to zoom 1 then to Parent 
-#if defined key_si3
-                             CALL Agrif_Update_ice( )   ! update sea-ice
-#endif
-      ENDIF
-#endif
-                             
+#endif                 
       !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
       ! Control
@@ -123,5 +117,18 @@
                              CALL dia_wri_state( 'output.abort' )
       ENDIF
-      IF( kstp == nit000   ) CALL iom_close( numror )     ! close input  ocean restart file
+#if defined key_agrif
+      !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+      ! AGRIF update
+      !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<      
+      IF( Agrif_NbStepint() == 0 .AND. nstop == 0 ) THEN                       ! AGRIF Update from zoom N to zoom 1 then to Parent 
+#if defined key_si3
+                             CALL Agrif_Update_ice( )   ! update sea-ice
+#endif
+      ENDIF
+#endif
+      !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+      ! File manipulation at the end of the first time step
+      !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<                         
+      IF( kstp == nit000   ) CALL iom_close( numror )                          ! close input  ocean restart file
       
       !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
