<commit_before>/*
 * author: <NAME> <<EMAIL>>
 */

#include "Glue.hxx"
#include "Client.hxx"
#include "Launch.hxx"
#include "Registry.hxx"
#include "system/Error.hxx"

#include <unistd.h>
#include <sys/socket.h>

SpawnServerClient *
StartSpawnServer(const SpawnConfig &config,
                 ChildProcessRegistry &child_process_registry,
                 std::function<void()> post_clone)
{
    int sv[2];
    if (socketpair(AF_LOCAL, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK,
                   0, sv) < 0)
        throw MakeErrno("socketpair() failed");

    const int close_fd = sv[1];

    pid_t pid = LaunchSpawnServer(config, sv[0],
                                  [close_fd, post_clone](){
                                      close(close_fd);
                                      post_clone();
                                  });
    if (pid < 0) {
        close(sv[0]);
        close(sv[1]);
        return nullptr;
    }

    child_process_registry.Add(pid, "spawn", nullptr);

    close(sv[0]);
    return new SpawnServerClient(config, sv[1]);
}
<commit_msg>spawn/Glue: fix LaunchSpawnServer() error check<commit_after>/*
 * author: <NAME> <<EMAIL>>
 */

#include "Glue.hxx"
#include "Client.hxx"
#include "Launch.hxx"
#include "Registry.hxx"
#include "system/Error.hxx"

#include <unistd.h>
#include <sys/socket.h>

SpawnServerClient *
StartSpawnServer(const SpawnConfig &config,
                 ChildProcessRegistry &child_process_registry,
                 std::function<void()> post_clone)
{
    int sv[2];
    if (socketpair(AF_LOCAL, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK,
                   0, sv) < 0)
        throw MakeErrno("socketpair() failed");

    const int close_fd = sv[1];

    pid_t pid;
    try {
        pid = LaunchSpawnServer(config, sv[0],
                                [close_fd, post_clone](){
                                    close(close_fd);
                                    post_clone();
                                });
    } catch (...) {
        close(sv[0]);
        close(sv[1]);
        throw;
    }

    child_process_registry.Add(pid, "spawn", nullptr);

    close(sv[0]);
    return new SpawnServerClient(config, sv[1]);
}

{'fraction_non_alphanumeric': 0.13154301319981793, 'fraction_numerical': 0.00864815657715066, 'mean_word_length': 1.978319783197832, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2440588', 'n_tokens_mistral': 719, 'n_tokens_neox': 697, 'n_words': 131}