Skip to content

VM top level vars are lost after a few iterations #769

Closed
@paleolitico

Description

@paleolitico

We use a DSL to configure a system.
We load it with vm.
Sometimes, for debuging purposes, we use top level variables in the DSL,
that are read and modified by several functions.
After a few executions of these functions, modifications done by one function
is not seen by others.
Tested with iojs-1.1.0.
It doesn't happen with node-0.12.0.

The following code reproduces the bug in a simplified framework.

The DSL executor:

var vm = require('vm');
var fs = require('fs');

function include(script) {
  vm.runInContext(fs.readFileSync(script), ctxt, script);
}

var jobs = [];
function job(name, n, action) {
  jobs.push({name: name, times: n, action: action});
}

var sandbox = {
  console: console,
  job: job
};

var ctxt = vm.createContext(sandbox);

function load() {
  for (var i = 2; i < process.argv.length; i++) include(process.argv[i]);
}

function execute(jobs) {
  for (var i = 0; i < 2; i++) {
    for (var j = 0; j < jobs.length; j++) {
      var job = jobs[j];
      var times = job.times;
      var action = job.action;
      console.info("Executing " + job.name + " (" + times + ")");
      for (var k = 0; k < times; k++) {
        action();
      }
    }
  }
}

load();
execute(jobs);

A configuration script to give as argument

var n = 0;

job("Action 1", 1000, function() {
  n++;
});

job("Action 2", 1, function() {
  console.info("Action 2 output: " + n);
  n=0;
});

Executed with node-0.12.0 the result is as expected:

Executing Action 1 (1000)
Executing Action 2 (1)
Action 2 output: 1000
Executing Action 1 (1000)
Executing Action 2 (1)
Action 2 output: 1000

Executed with iojs-1.1.0 the result is

Executing Action 1 (1000)
Executing Action 2 (1)
Action 2 output: 517
Executing Action 1 (1000)
Executing Action 2 (1)
Action 2 output: 0

The value 517 differs on each execution.
I suppose it depends on the V8 optimization process.

Probably related to #548
and nodejs/node-v0.x-archive#9084

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.vmIssues and PRs related to the vm subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions