Skip to content

Commit b3eafd5

Browse files
committed
Add strings to indicate when the process is done
1 parent 97b676e commit b3eafd5

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

cwltool/cwlNodeEngine.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ process.stdin.on("data", function(chunk) {
55
incoming += chunk;
66
var i = incoming.indexOf("\n");
77
if (i > -1) {
8-
var fn = JSON.parse(incoming.substr(0, i));
9-
incoming = incoming.substr(i+1);
10-
process.stdout.write(JSON.stringify(require("vm").runInNewContext(fn, {})) + "\n");
8+
try{
9+
var fn = JSON.parse(incoming.substr(0, i));
10+
incoming = incoming.substr(i+1);
11+
process.stdout.write(JSON.stringify(require("vm").runInNewContext(fn, {})) + "\n");
12+
}
13+
finally{
14+
/*strings to indicate the process has finished*/
15+
console.log("r1cepzbhUTxtykz5XTC4");
16+
console.error("r1cepzbhUTxtykz5XTC4");
17+
}
1118
}
1219
});
1320
process.stdin.on("end", process.exit);

cwltool/cwlNodeEngineJSConsole.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@ process.stdin.on("data", function(chunk) {
1111
incoming += chunk;
1212
var i = incoming.indexOf("\n");
1313
if (i > -1) {
14-
var fn = JSON.parse(incoming.substr(0, i));
15-
incoming = incoming.substr(i+1);
16-
process.stdout.write(JSON.stringify(require("vm").runInNewContext(fn, {
17-
console: {
18-
log: js_console_log,
19-
error: js_console_err
20-
}
21-
})) + "\n");
14+
try{
15+
var fn = JSON.parse(incoming.substr(0, i));
16+
incoming = incoming.substr(i+1);
17+
process.stdout.write(JSON.stringify(require("vm").runInNewContext(fn, {
18+
console: {
19+
log: js_console_log,
20+
error: js_console_err
21+
}
22+
})) + "\n");
23+
}
24+
finally{
25+
/*strings to indicate the process has finished*/
26+
console.log("r1cepzbhUTxtykz5XTC4");
27+
console.error("r1cepzbhUTxtykz5XTC4");
28+
}
2229
}
2330
});
2431
process.stdin.on("end", process.exit);

cwltool/sandboxjs.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ def terminate():
164164
rselect = [nodejs.stdout, nodejs.stderr] # type: List[BytesIO]
165165
wselect = [nodejs.stdin] # type: List[BytesIO]
166166

167+
PROCESS_FINISHED_STR = "r1cepzbhUTxtykz5XTC4\n"
168+
169+
def process_finished():
170+
return stdout_buf.getvalue().decode().endswith(PROCESS_FINISHED_STR) and \
171+
stderr_buf.getvalue().decode().endswith(PROCESS_FINISHED_STR)
172+
167173
# On windows system standard input/output are not handled properly by select module
168174
# (modules like pywin32, msvcrt, gevent don't work either)
169175
if sys.platform=='win32':
@@ -214,7 +220,9 @@ def get_error(error_queue):
214220
error_thread.daemon=True
215221
error_thread.start()
216222

217-
while (len(wselect) + len(rselect)) > 0:
223+
finished = False
224+
225+
while not finished and tm.isAlive():
218226
try:
219227
if nodejs.stdin in wselect:
220228
if not input_queue.empty():
@@ -227,41 +235,35 @@ def get_error(error_queue):
227235

228236
if nodejs.stderr in rselect:
229237
if not error_queue.empty():
230-
stderr_buf.write(error_queue.get())
238+
stderr_buf.write(error_queue.get())
231239

232-
if stdout_buf.getvalue().endswith("\n".encode()):
233-
rselect = []
240+
if process_finished() and error_queue.empty() and output_queue.empty():
241+
finished = True
234242
no_more_output.release()
235243
no_more_error.release()
236244
except OSError as e:
237245
break
238246

239247
else:
240-
while (len(wselect) + len(rselect)) > 0:
248+
while not (process_finished() or not tm.isAlive()):
241249
rready, wready, _ = select.select(rselect, wselect, [])
242250
try:
243251
if nodejs.stdin in wready:
244252
b = stdin_buf.read(select.PIPE_BUF)
245253
if b:
246254
os.write(nodejs.stdin.fileno(), b)
247-
else:
248-
wselect = []
249255
for pipes in ((nodejs.stdout, stdout_buf), (nodejs.stderr, stderr_buf)):
250256
if pipes[0] in rready:
251257
b = os.read(pipes[0].fileno(), select.PIPE_BUF)
252258
if b:
253259
pipes[1].write(b)
254-
else:
255-
rselect.remove(pipes[0])
256-
if stdout_buf.getvalue().endswith("\n".encode()):
257-
rselect = []
258260
except OSError as e:
259261
break
260262
tm.cancel()
261263

262264
stdin_buf.close()
263-
stdoutdata = stdout_buf.getvalue()
264-
stderrdata = stderr_buf.getvalue()
265+
stdoutdata = stdout_buf.getvalue()[:-len(PROCESS_FINISHED_STR) - 1]
266+
stderrdata = stderr_buf.getvalue()[:-len(PROCESS_FINISHED_STR) - 1]
265267

266268
def fn_linenum(): # type: () -> Text
267269
lines = fn.splitlines()

0 commit comments

Comments
 (0)