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.
This commit is contained in:
Andrew Kaster 2023-03-26 09:39:52 -06:00 committed by Linus Groh
parent af118abdf0
commit afb3a4a030
Notes: sideshowbarker 2024-07-16 23:05:02 +09:00

View file

@ -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 {};
}