@@ -3981,13 +3981,18 @@ void dd_load_tablespace(const Table *dd_table, dict_table_t *table,
39813981    space_name = tablespace_name.c_str ();
39823982  }
39833983
3984+   auto  is_already_opened = [&]() {
3985+     if  (fil_space_exists_in_mem (table->space , space_name, false , true , heap,
3986+                                 table->id )) {
3987+       dd_get_and_save_data_dir_path (table, dd_table, true );
3988+       ut_free (shared_space_name);
3989+       return  true ;
3990+     }
3991+     return  false ;
3992+   };
3993+ 
39843994  /*  The tablespace may already be open. */ 
3985-   if  (fil_space_exists_in_mem (table->space , space_name, false , true , heap,
3986-                               table->id )) {
3987-     dd_get_and_save_data_dir_path (table, dd_table, true );
3988-     ut_free (shared_space_name);
3989-     return ;
3990-   }
3995+   if  (is_already_opened ()) return ;
39913996
39923997  if  (!(ignore_err & DICT_ERR_IGNORE_RECOVER_LOCK)) {
39933998    ib::error (ER_IB_MSG_172)
@@ -4019,6 +4024,7 @@ void dd_load_tablespace(const Table *dd_table, dict_table_t *table,
40194024  path is changed then boot_tablespaces() would always open the tablespace. */  
40204025  mutex_exit (&dict_sys->mutex );
40214026  filepath = dd_get_first_path (heap, table, dd_table);
4027+   DEBUG_SYNC_C (" innodb_dd_load_tablespace_no_dict_mutex"  );
40224028  mutex_enter (&dict_sys->mutex );
40234029
40244030  if  (filepath == nullptr ) {
@@ -4027,6 +4033,12 @@ void dd_load_tablespace(const Table *dd_table, dict_table_t *table,
40274033                            << table->space  << "  in the data dictionary."  ;
40284034  }
40294035
4036+   /*  The tablespace may have been opened while we released the dict_sys mutex.
4037+   We need to check again if it was not opened in meantime, as fil_ibd_open 
4038+   will try to close it forcefully, even if some IO is underway, leading to 
4039+   crash. */  
4040+   if  (is_already_opened ()) return ;
4041+ 
40304042  /*  Try to open the tablespace.  We set the 2nd param (fix_dict) to
40314043  false because we do not have an x-lock on dict_operation_lock */  
40324044  dberr_t  err =
@@ -4295,6 +4307,8 @@ dict_table_t *dd_open_table_one(dd::cache::Dictionary_client *client,
42954307      dd_load_tablespace (dd_table, m_table, heap, DICT_ERR_IGNORE_RECOVER_LOCK,
42964308                         dd_fsp_flags);
42974309
4310+       DEBUG_SYNC_C (" innodb_dd_load_tablespace_done"  );
4311+ 
42984312      if  (dd_space && m_table->space  != TRX_SYS_SPACE &&
42994313          fil_space_get (m_table->space ) != nullptr ) {
43004314        /*  Get the autoextend_size property from the tablespace
0 commit comments