1'use strict'; 2require('../common'); 3const vm = require('vm'); 4 5// We're testing a race condition so we just have to spin this in a loop 6// for a little while and see if it breaks. The condition being tested 7// is an `isolate->TerminateExecution()` reaching the main JS stack from 8// the timeout watchdog. 9const sandbox = { timeout: 5 }; 10const context = vm.createContext(sandbox); 11const script = new vm.Script( 12 'var d = Date.now() + timeout;while (d > Date.now());', 13); 14const immediate = setImmediate(function() { 15 throw new Error('Detected vm race condition!'); 16}); 17 18// When this condition was first discovered this test would fail in 50ms 19// or so. A better, but still incorrect implementation would fail after 20// 100 seconds or so. If you're messing with vm timeouts you might 21// consider increasing this timeout to hammer out races. 22const giveUp = Date.now() + 5000; 23do { 24 // The loop adjusts the timeout up or down trying to hit the race 25 try { 26 script.runInContext(context, { timeout: 5 }); 27 ++sandbox.timeout; 28 } catch { 29 --sandbox.timeout; 30 } 31} while (Date.now() < giveUp); 32 33clearImmediate(immediate); 34