Skip to content

Commit 7b12436

Browse files
azeeyarjo129
andcommitted
Fix running server and GUI together on macOS (#3405)
On macOS the Metal API requires initialization from the main thread. As a result, we currently cannot run worlds with rendering sensors on macOS without running the server and GUI seprately. This PR fixes this be ensuring the server runs on the main thread (using the blocking=true flag on Server::Run) and creating a separate thread to monitor the GUI process. Generated-By: Gemini 2.5 Pro Signed-off-by: Addisu Z. Taddese <addisuzt@intrinsic.ai> Co-authored-by: Arjo Chakravarty <arjoc@intrinsic.ai> (cherry picked from commit 7dd25f6)
1 parent 21c17dd commit 7b12436

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/cmd/sim_main.cc

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -483,11 +483,8 @@ int main(int argc, char** argv)
483483
return -1;
484484
}
485485

486-
bool blocking = true;
487-
488486
#ifdef WITH_GUI
489487
opt->waitGui = 1;
490-
blocking = false;
491488

492489
// Launch the GUI in a separate thread
493490
std::thread guiThread(
@@ -525,20 +522,27 @@ int main(int argc, char** argv)
525522
return -1;
526523
}
527524

528-
// Run the server in a separate thread
529525
sim::Server server(serverConfig);
530-
server.Run(blocking, opt->iterations, opt->runOnStart == 0);
531526

532527
#ifdef WITH_GUI
533-
// Join the GUI thread to wait for a possible window close
534-
if (guiThread.joinable())
535-
guiThread.join();
528+
// Watch for the GUI to close, and stop the server if it does.
529+
std::thread watcherThread([&]{
530+
// Wait for the GUI thread to finish, indicating the GUI has closed
531+
if (guiThread.joinable())
532+
guiThread.join();
536533

537-
// Shutdown server if the GUI has been closed from the screen
538-
if(server.Running())
539-
{
540-
server.Stop();
541-
}
534+
if(server.Running())
535+
{
536+
server.Stop();
537+
}
538+
});
539+
#endif
540+
541+
// Run the server on the main thread. This is a blocking call.
542+
server.Run(true, opt->iterations, opt->runOnStart == 0);
543+
544+
#ifdef WITH_GUI
545+
watcherThread.join();
542546
#endif
543547

544548
gzdbg << "Shutting down gz-sim-server" << std::endl;

0 commit comments

Comments
 (0)