Skip to content

Commit 03b84d2

Browse files
Kyle KeatingLMS007
authored andcommitted
Fix CodeRun
- CodeRun has two module vars which were global to the module, but they need to be scoped to the instance of the CodeRun class currently running otherwise multiple CodeRuns will complete and use the same vars. This could happen if a work execute 2 or more tests with CodeRuns in the same batch or a single test which has multiple CodeRun steps. This could also happen with multiple calls to child tests with CodeRuns. - Additionally, the uncaughtException was not fully working because node needs the `unhandled-rejections=strict` param which will trigger this event when an uncaught exception is thrown. - Move the uncaughtException callback to be inside the CodeRun eval function so it can bind with the scope of the function and have access to `this` so it can call this.resolve() and fail the test with the uncaughtException error message from node.
1 parent 51c230d commit 03b84d2

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"private": false,
66
"license": "MIT",
77
"scripts": {
8-
"worker:local:interval": "node src/main.js",
8+
"worker:local:interval": "node --unhandled-rejections=strict src/main.js",
99
"lint": "yarn eslint src/**",
1010
"format": "yarn prettier -w src/**",
1111
"check-format": "yarn prettier -c src/**",

src/models/actions/CodeRun.js

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,36 @@ const { BaseAction } = require("./BaseAction");
22
const { performance } = require("perf_hooks");
33
const { NodeVM } = require("vm2");
44

5-
let resolve = null;
6-
let t0 = 0;
7-
8-
process.on("uncaughtException", (e) => {
9-
if (resolve) {
10-
resolve({
11-
actionReports: [
12-
{
13-
action: "Code.run",
14-
success: false,
15-
shortSummary: e.message || e,
16-
longSummary: null,
17-
time: performance.now() - t0,
18-
},
19-
],
20-
});
5+
class CodeRun extends BaseAction {
6+
constructor(args) {
7+
super(args);
8+
this.resolve = null;
9+
this.t0 = 0;
2110
}
22-
// Do not delete!
23-
//
24-
// Used to catch asynchronous exceptions from inside of CodeRun that will otherwise
25-
// not be caught by try/catch.
26-
//
27-
// The worker will crash if this event is not defined and an exception occurs in an
28-
// asynchronous code run.
29-
});
3011

31-
class CodeRun extends BaseAction {
3212
async eval(context) {
13+
process.on("uncaughtException", (e) => {
14+
// Used to catch asynchronous unhandled exceptions from inside of CodeRun.
15+
// https://github.com/patriksimek/vm2#error-handling
16+
if (this.resolve) {
17+
this.resolve({
18+
actionReports: [
19+
{
20+
action: "Code.run",
21+
success: false,
22+
shortSummary: e.message || e,
23+
longSummary: null,
24+
time: performance.now() - this.t0,
25+
},
26+
],
27+
});
28+
}
29+
});
30+
3331
return new Promise((_resolve) => {
34-
resolve = _resolve;
35-
t0 = performance.now();
32+
this.resolve = _resolve;
33+
this.kyle = 1;
34+
this.t0 = performance.now();
3635
const vm = new NodeVM({
3736
timeout: 60000,
3837
eval: false,
@@ -69,7 +68,7 @@ class CodeRun extends BaseAction {
6968
});
7069

7170
// happy path
72-
resolve({
71+
this.resolve({
7372
contextWrites,
7473
actionReports: [
7574
{
@@ -83,35 +82,35 @@ class CodeRun extends BaseAction {
8382
null,
8483
4
8584
),
86-
time: performance.now() - t0,
85+
time: performance.now() - this.t0,
8786
},
8887
],
8988
});
9089
})
9190
.catch((e) => {
9291
// error inside async code run that returns a promise
93-
resolve({
92+
this.resolve({
9493
actionReports: [
9594
{
9695
action: "Code.run",
9796
success: false,
9897
shortSummary: e.message || e,
9998
longSummary: null,
100-
time: performance.now() - t0,
99+
time: performance.now() - this.t0,
101100
},
102101
],
103102
});
104103
});
105104
} catch (e) {
106105
// error inside sync code run that returns an object
107-
resolve({
106+
this.resolve({
108107
actionReports: [
109108
{
110109
action: "Code.run",
111110
success: false,
112111
shortSummary: e.message || e,
113112
longSummary: null,
114-
time: performance.now() - t0,
113+
time: performance.now() - this.t0,
115114
},
116115
],
117116
});

0 commit comments

Comments
 (0)