@@ -107,10 +107,29 @@ static void PlaygroundKeyCallback(GLFWwindow* window,
107107 ImGui::StyleColorsDark ();
108108 ImGui::GetIO ().IniFilename = nullptr ;
109109
110- if (::glfwInit () != GLFW_TRUE) {
111- return false ;
110+ // This guard is a hack to work around a problem where glfwCreateWindow
111+ // hangs when opening a second window after GLFW has been reinitialized (for
112+ // example, when flipping through multiple playground tests).
113+ //
114+ // Explanation:
115+ // * glfwCreateWindow calls [NSApp run], which begins running the event loop
116+ // on the current thread.
117+ // * GLFW then immediately stops the loop when applicationDidFinishLaunching
118+ // is fired.
119+ // * applicationDidFinishLaunching is only ever fired once during the
120+ // application's lifetime, so subsequent calls to [NSApp run] will always
121+ // hang with this setup.
122+ // * glfwInit resets the flag that guards against [NSApp run] being
123+ // called a second time, which causes the subsequent `glfwCreateWindow` to
124+ // hang indefinitely in the event loop, because
125+ // applicationDidFinishLaunching is never fired.
126+ static bool first_run = true ;
127+ if (first_run) {
128+ first_run = false ;
129+ if (::glfwInit () != GLFW_TRUE) {
130+ return false ;
131+ }
112132 }
113- fml::ScopedCleanupClosure terminate ([]() { ::glfwTerminate (); });
114133
115134 ::glfwWindowHint (GLFW_CLIENT_API, GLFW_NO_API);
116135
0 commit comments