<commit_before><?php

class CM_Process_ForkHandler {

    /** @var int */
    private $_pid;

    /** @var Closure */
    private $_workload;

    /** @var resource */
    private $_ipcStream;

    /**
     * @param int      $pid
     * @param Closure  $workload
     * @param resource $ipcStream
     */
    public function __construct($pid, Closure $workload, $ipcStream) {
        $this->_pid = (int) $pid;
        $this->_workload = $workload;
        $this->_ipcStream = $ipcStream;
    }

    /**
     * @return int
     */
    public function getPid() {
        return $this->_pid;
    }

    /**
     * @return Closure
     */
    public function getWorkload() {
        return $this->_workload;
    }

    /**
     * @return resource
     */
    public function getIpcStream() {
        return $this->_ipcStream;
    }

    public function closeIpcStream() {
        fclose($this->_ipcStream);
    }

    /**
     * @throws Exception
     */
    public function runAndSendWorkload() {
        $workload = $this->_workload;
        $return = null;
        $exception = null;
        try {
            $return = $workload();
        } catch (Exception $e) {
            $exception = $e;
        }

        $result = new CM_Process_WorkloadResult($return, $exception);
        fwrite($this->_ipcStream, serialize($result));

        if (null !== $exception) {
            throw $exception;
        }
    }

    /**
     * @return CM_Process_WorkloadResult
     * @throws CM_Exception
     */
    public function receiveWorkloadResult() {
        $ipcData = fgets($this->_ipcStream);
        if (false === $ipcData) {
            throw new CM_Exception('Received no data from IPC stream.');
        }
        $invalidIpcDataException = new CM_Exception('Received unexpected data over IPC stream.', [
            'ipcData' => $ipcData
        ]);
        try {
            $workloadResult = unserialize($ipcData);
        } catch (CM_Exception_Invalid $e) {
            throw $invalidIpcDataException;
        }
        if (!$workloadResult instanceof CM_Process_WorkloadResult) {
            throw $invalidIpcDataException;
        }
        return $workloadResult;
    }
}<commit_msg>Log ipcData into exception meta data<commit_after><?php

class CM_Process_ForkHandler {

    /** @var int */
    private $_pid;

    /** @var Closure */
    private $_workload;

    /** @var resource */
    private $_ipcStream;

    /**
     * @param int      $pid
     * @param Closure  $workload
     * @param resource $ipcStream
     */
    public function __construct($pid, Closure $workload, $ipcStream) {
        $this->_pid = (int) $pid;
        $this->_workload = $workload;
        $this->_ipcStream = $ipcStream;
    }

    /**
     * @return int
     */
    public function getPid() {
        return $this->_pid;
    }

    /**
     * @return Closure
     */
    public function getWorkload() {
        return $this->_workload;
    }

    /**
     * @return resource
     */
    public function getIpcStream() {
        return $this->_ipcStream;
    }

    public function closeIpcStream() {
        fclose($this->_ipcStream);
    }

    /**
     * @throws Exception
     */
    public function runAndSendWorkload() {
        $workload = $this->_workload;
        $return = null;
        $exception = null;
        try {
            $return = $workload();
        } catch (Exception $e) {
            $exception = $e;
        }

        $result = new CM_Process_WorkloadResult($return, $exception);
        fwrite($this->_ipcStream, serialize($result));

        if (null !== $exception) {
            throw $exception;
        }
    }

    /**
     * @return CM_Process_WorkloadResult
     * @throws CM_Exception
     */
    public function receiveWorkloadResult() {
        $ipcData = fgets($this->_ipcStream);
        if (false === $ipcData) {
            throw new CM_Exception('Received no data from IPC stream.');
        }
        $invalidIpcDataException = new CM_Exception('Received unexpected data over IPC stream.', [
            'ipcData' => $ipcData
        ]);
        try {
            $workloadResult = unserialize($ipcData);
        } catch (ErrorException $e) {
            throw $invalidIpcDataException;
        }
        if (!$workloadResult instanceof CM_Process_WorkloadResult) {
            throw $invalidIpcDataException;
        }
        return $workloadResult;
    }
}
{'fraction_non_alphanumeric': 0.12417557425517398, 'fraction_numerical': 0.0, 'mean_word_length': 1.8858267716535433, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '2344760', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1281, 'n_words': 281}