Browse Source

LibSQL: Block signals while forking SQLServer in Lagom

When debugging in Xcode, the waitpid() for the initial forked process
would always return EINTR or ECHILD. Work around this by blocking all
signals until we're ready to wait for the initial child.
Andrew Kaster 2 năm trước cách đây
mục cha
commit
afb3a4a030
1 tập tin đã thay đổi với 11 bổ sung1 xóa
  1. 11 1
      Userland/Libraries/LibSQL/SQLClient.cpp

+ 11 - 1
Userland/Libraries/LibSQL/SQLClient.cpp

@@ -15,6 +15,7 @@
 #    include <LibCore/StandardPaths.h>
 #    include <LibCore/System.h>
 #    include <LibFileSystem/FileSystem.h>
+#    include <signal.h>
 #endif
 
 namespace SQL {
@@ -58,9 +59,14 @@ static ErrorOr<void> launch_server(DeprecatedString const& socket_path, Deprecat
         return server_fd_or_error.release_error();
     }
     auto server_fd = server_fd_or_error.value();
+    sigset_t original_set;
+    sigset_t setting_set;
+    sigfillset(&setting_set);
+    (void)pthread_sigmask(SIG_BLOCK, &setting_set, &original_set);
     auto server_pid = TRY(Core::System::fork());
 
     if (server_pid == 0) {
+        (void)pthread_sigmask(SIG_SETMASK, &original_set, nullptr);
         TRY(Core::System::setsid());
         TRY(Core::System::signal(SIGCHLD, SIG_IGN));
         server_pid = TRY(Core::System::fork());
@@ -95,8 +101,12 @@ static ErrorOr<void> launch_server(DeprecatedString const& socket_path, Deprecat
 
         VERIFY_NOT_REACHED();
     }
+    VERIFY(server_pid > 0);
 
-    TRY(Core::System::waitpid(server_pid));
+    auto wait_err = Core::System::waitpid(server_pid);
+    (void)pthread_sigmask(SIG_SETMASK, &original_set, nullptr);
+    if (wait_err.is_error())
+        return wait_err.release_error();
     return {};
 }