Fix more rare race conditions. Use a global mutex for all the open/close