Fix a race condition between stdout/stderr prints vs exit() when using --emrun: fully wait until all stdout/stderr messages have been sent before closing the Firefox browser window.

This commit is contained in:
Jukka Jylänki 2015-09-07 20:52:58 +03:00
Родитель 1217bf6ca8
Коммит f195fcb721
1 изменённых файлов: 17 добавлений и 6 удалений

Просмотреть файл

@ -1,10 +1,10 @@
if (typeof window === "object" && (typeof ENVIRONMENT_IS_PTHREAD === 'undefined' || !ENVIRONMENT_IS_PTHREAD)) {
function emrun_register_handlers() {
function post(msg) {
var http = new XMLHttpRequest();
http.open("POST", "stdio.html", true);
http.send(msg);
}
// When C code exit()s, we may still have remaining stdout and stderr messages in flight. In that case, we can't close
// the browser until all those XHRs have finished, so the following state variables track that all communication is done,
// after which we can close.
var emrun_num_post_messages_in_flight = 0;
var emrun_should_close_itself = false;
function postExit(msg) {
var http = new XMLHttpRequest();
http.onreadystatechange = function() {
@ -19,12 +19,23 @@ if (typeof window === "object" && (typeof ENVIRONMENT_IS_PTHREAD === 'undefined'
http.open("POST", "stdio.html", true);
http.send(msg);
}
function post(msg) {
var http = new XMLHttpRequest();
++emrun_num_post_messages_in_flight;
http.onreadystatechange = function() {
if (http.readyState == 4 /*DONE*/) {
if (--emrun_num_post_messages_in_flight == 0 && emrun_should_close_itself) postExit('^exit^'+EXITSTATUS);
}
}
http.open("POST", "stdio.html", true);
http.send(msg);
}
// If the address contains localhost, or we are running the page from port 6931, we can assume we're running the test runner and should post stdout logs.
if (document.URL.search("localhost") != -1 || document.URL.search(":6931/") != -1) {
var emrun_http_sequence_number = 1;
var prevPrint = Module['print'];
var prevErr = Module['printErr'];
function emrun_exit() { postExit('^exit^'+EXITSTATUS); };
function emrun_exit() { if (emrun_num_post_messages_in_flight == 0) postExit('^exit^'+EXITSTATUS); else emrun_should_close_itself = true; };
Module['addOnExit'](emrun_exit);
Module['print'] = function emrun_print(text) { post('^out^'+(emrun_http_sequence_number++)+'^'+encodeURIComponent(text)); prevPrint(text); }
Module['printErr'] = function emrun_printErr(text) { post('^err^'+(emrun_http_sequence_number++)+'^'+encodeURIComponent(text)); prevErr(text); }