<commit_before>package javarepl.web;

import com.googlecode.utterlyidle.RequestBuilder;
import com.googlecode.utterlyidle.Response;
import com.googlecode.utterlyidle.Status;
import com.googlecode.utterlyidle.handlers.ClientHttpHandler;
import javarepl.Main;

import java.io.File;
import java.util.UUID;

import static com.googlecode.totallylazy.Sequences.sequence;
import static com.googlecode.utterlyidle.Responses.response;
import static com.googlecode.utterlyidle.Status.GATEWAY_TIMEOUT;
import static com.googlecode.utterlyidle.Status.INTERNAL_SERVER_ERROR;
import static java.io.File.pathSeparator;
import static java.lang.Thread.sleep;
import static java.net.URLDecoder.decode;
import static javarepl.Utils.randomServerPort;
import static javarepl.console.TimingOutConsole.EXPRESSION_TIMEOUT;
import static javarepl.console.TimingOutConsole.INACTIVITY_TIMEOUT;

public class WebConsoleClientHandler {
    private final String id;

    private int port;
    private Process process;

    public WebConsoleClientHandler() {
        this.id = UUID.randomUUID().toString();
    }

    public String id() {
        return id;
    }

    public int port() {
        return port;
    }

    private void createProcess() {
        if (process == null) {
            try {
                File path = new File(decode(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath(), "ISO-8859-1"));
                String classpath = sequence(System.getProperty("java.class.path")).add(path.toURI().toURL().toString()).toString(pathSeparator);
                port = randomServerPort();
                ProcessBuilder builder = new ProcessBuilder("java", "-Xmx96M", "-cp", classpath, Main.class.getCanonicalName(),
                        "--sandboxed", "--ignoreConsole", "--port=" + port, "--expressionTimeout=15", "--inactivityTimeout=300");
                builder.redirectErrorStream(true);
                process = builder.start();

                sleep(1000);

            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void shutdown() {
        if (process != null) {
            process.destroy();
            process = null;
        }
    }

    public Response execute(String expression) {
        createProcess();

        try {
            return reportProcessError(new ClientHttpHandler().handle(RequestBuilder.post("http://localhost:" + port + "/" + "execute").form("expression", expression).build()));
        } catch (Exception e) {
            return response(INTERNAL_SERVER_ERROR);
        }
    }

    private Response reportProcessError(Response response) {
        if (response.status() == Status.OK)
            return response;

        int exitCode = waitForProcessToExit();
        switch (exitCode) {
            case EXPRESSION_TIMEOUT:
                return response(GATEWAY_TIMEOUT);
            case INACTIVITY_TIMEOUT:
                return response(GATEWAY_TIMEOUT);
            default:
                return response;
        }
    }

    private int waitForProcessToExit() {
        for (int attempt = 0; attempt < 10; attempt++) {
            try {
                Thread.sleep(100);
                return process.exitValue();
            } catch (Exception e) {
            }
        }

        return -1;
    }
}
<commit_msg>making sure process is shutdown properly<commit_after>package javarepl.web;

import com.googlecode.utterlyidle.RequestBuilder;
import com.googlecode.utterlyidle.Response;
import com.googlecode.utterlyidle.Status;
import com.googlecode.utterlyidle.handlers.ClientHttpHandler;
import javarepl.Main;

import java.io.File;
import java.util.UUID;

import static com.googlecode.totallylazy.Sequences.sequence;
import static com.googlecode.utterlyidle.Responses.response;
import static com.googlecode.utterlyidle.Status.GATEWAY_TIMEOUT;
import static com.googlecode.utterlyidle.Status.INTERNAL_SERVER_ERROR;
import static java.io.File.pathSeparator;
import static java.lang.Thread.sleep;
import static java.net.URLDecoder.decode;
import static javarepl.Utils.randomServerPort;
import static javarepl.console.TimingOutConsole.EXPRESSION_TIMEOUT;
import static javarepl.console.TimingOutConsole.INACTIVITY_TIMEOUT;

public class WebConsoleClientHandler {
    private final String id;

    private int port;
    private Process process;

    public WebConsoleClientHandler() {
        this.id = UUID.randomUUID().toString();
    }

    public String id() {
        return id;
    }

    public int port() {
        return port;
    }

    private void createProcess() {
        if (process == null) {
            try {
                File path = new File(decode(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath(), "ISO-8859-1"));
                String classpath = sequence(System.getProperty("java.class.path")).add(path.toURI().toURL().toString()).toString(pathSeparator);
                port = randomServerPort();
                ProcessBuilder builder = new ProcessBuilder("java", "-Xmx96M", "-cp", classpath, Main.class.getCanonicalName(),
                        "--sandboxed", "--ignoreConsole", "--port=" + port, "--expressionTimeout=15", "--inactivityTimeout=300");
                builder.redirectErrorStream(true);
                process = builder.start();

                sleep(1000);

            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void shutdown() {
        if (process != null) {
            process.destroy();
            try {
                process.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            process = null;
        }
    }

    public Response execute(String expression) {
        createProcess();

        try {
            return reportProcessError(new ClientHttpHandler().handle(RequestBuilder.post("http://localhost:" + port + "/" + "execute").form("expression", expression).build()));
        } catch (Exception e) {
            return response(INTERNAL_SERVER_ERROR);
        }
    }

    private Response reportProcessError(Response response) {
        if (response.status() == Status.OK)
            return response;

        int exitCode = waitForProcessToExit();
        switch (exitCode) {
            case EXPRESSION_TIMEOUT:
            case INACTIVITY_TIMEOUT: {
                shutdown();
                return response(GATEWAY_TIMEOUT);
            }
            default:
                return response;
        }
    }

    private int waitForProcessToExit() {
        for (int attempt = 0; attempt < 10; attempt++) {
            try {
                Thread.sleep(100);
                return process.exitValue();
            } catch (Exception e) {
            }
        }

        return -1;
    }
}

{'fraction_non_alphanumeric': 0.10748809695570624, 'fraction_numerical': 0.006636848939546963, 'mean_word_length': 2.348792270531401, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2131515', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1824, 'n_words': 389}