<commit_before>/**
 * Created by AlexanderC on 1/21/16.
 */

'use strict';

import domain from 'domain';
import process from 'process';

export class Sandbox {
  /**
   * @param {Function} func
   */
  constructor(func) {
    this._func = func;
    this._onFail = Sandbox.ON_FAIL_CB;
  }

  /**
   * @returns {Function}
   */
  get func() {
    return this._func;
  }

  /**
   * @returns {Sandbox}
   */
  run(...args) {
    let execDomain = domain.create();

    let failCb = (error) => {
      try {
        execDomain.exit();
      } catch (e) {/* silent fail */}

      setImmediate(() => {
        this._onFail(error);
      });
    };

    execDomain.once('error', failCb);
    // domain "unhandledRejection" are throw in global scope
    process.on('unhandledRejection', failCb);

    try {
      execDomain.run(this._func, ...args);
    } catch (error) {
      failCb(error);<commit_msg>Fix lambda memory leak<commit_after>/**
 * Created by AlexanderC on 1/21/16.
 */

'use strict';

import domain from 'domain';
import process from 'process';

export class Sandbox {
  /**
   * @param {Function} func
   */
  constructor(func) {
    this._func = func;
    this._onFail = Sandbox.ON_FAIL_CB;
  }

  /**
   * @returns {Function}
   */
  get func() {
    return this._func;
  }

  /**
   * @returns {Sandbox}
   */
  run(...args) {
    let execDomain = domain.create();

    let failCb = (error) => {
      try {
        process.removeListener('unhandledRejection', failCb);
        execDomain.exit();
      } catch (e) {/* silent fail */}

      setImmediate(() => {
        this._onFail(error);
      });
    };

    execDomain.once('error', failCb);
    // domain "unhandledRejection" are throw in global scope
    process.on('unhandledRejection', failCb);

    try {
      execDomain.run(this._func, ...args);
    } catch (error) {
      failCb(error);
{'fraction_non_alphanumeric': 0.1615673644659152, 'fraction_numerical': 0.005367686527106817, 'mean_word_length': 2.310834813499112, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '232342', 'n_tokens_mistral': 645, 'n_tokens_neox': 624, 'n_words': 138}