Skip to content

Commit 01fe931

Browse files
committed
Try fix exec_js with context in windows
1 parent 0b514c8 commit 01fe931

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

cwltool/cwlNodeEngineWithContext.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,36 @@
22
process.stdin.setEncoding("utf8");
33
var incoming = "";
44
var firstInput = true;
5-
var context;
5+
var context = {};
66

77
process.stdin.on("data", function(chunk) {
88
incoming += chunk;
99
var i = incoming.indexOf("\n");
10-
if (i > -1) {
10+
while (i > -1) {
1111
try{
12-
var fn = JSON.parse(incoming.substr(0, i));
12+
var input = incoming.substr(0, i);
1313
incoming = incoming.substr(i+1);
14+
var fn = JSON.parse(input);
1415
if(firstInput){
1516
context = require("vm").runInNewContext(fn, {});
16-
firstInput = false;
1717
}
1818
else{
1919
process.stdout.write(JSON.stringify(require("vm").runInNewContext(fn, context)) + "\n");
2020
}
2121
}
2222
catch(e){
23-
console.error(e)
23+
console.error(e);
2424
}
25-
/*strings to indicate the process has finished*/
26-
console.log("r1cepzbhUTxtykz5XTC4");
27-
console.error("r1cepzbhUTxtykz5XTC4");
25+
if(firstInput){
26+
firstInput = false;
27+
}
28+
else{
29+
/*strings to indicate the process has finished*/
30+
console.log("r1cepzbhUTxtykz5XTC4");
31+
console.error("r1cepzbhUTxtykz5XTC4");
32+
}
33+
34+
i = incoming.indexOf("\n");
2835
}
2936
});
3037
process.stdin.on("end", process.exit);

cwltool/sandboxjs.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,18 @@ def exec_js_process(js_text, timeout=None, js_console=False, context=None, force
140140
else:
141141
js_engine = 'cwlNodeEngine.js'
142142

143-
if localdata.procs.get(js_engine) is None \
143+
created_new_process = False
144+
145+
if (context is None and localdata.procs.get(js_engine) is None) \
146+
or (context is not None and localdata.procs.get((js_engine, context)) is None) \
144147
or localdata.procs[js_engine].poll() is not None \
145148
or onWindows():
146149
res = resource_stream(__name__, js_engine)
147150
js_engine_code = res.read().decode('utf-8')
148151

152+
created_new_process = True
153+
149154
localdata.procs[js_engine] = new_js_proc(js_engine_code, force_docker_pull=force_docker_pull)
150-
if context is not None:
151-
output = exec_js_process(context, timeout=timeout, js_console=js_console, context=context, force_docker_pull=force_docker_pull, debug=debug)
152-
if output != (0, "", ""):
153-
# restart to reset the context
154-
localdata.procs[js_engine].kill()
155-
del localdata.procs[js_engine]
156-
return output
157155

158156
nodejs = localdata.procs[js_engine]
159157

@@ -173,7 +171,12 @@ def terminate():
173171
tm = threading.Timer(timeout, terminate)
174172
tm.start()
175173

176-
stdin_buf = BytesIO((json.dumps(js_text) + "\n").encode('utf-8'))
174+
stdin_text = ""
175+
if created_new_process and context is not None:
176+
stdin_text = json.dumps(context) + "\n"
177+
stdin_text += json.dumps(js_text) + "\n"
178+
179+
stdin_buf = BytesIO(stdin_text.encode('utf-8'))
177180
stdout_buf = BytesIO()
178181
stderr_buf = BytesIO()
179182

0 commit comments

Comments
 (0)