Skip to content

Commit 38e8a90

Browse files
committed
Fixing race condition in handling unhandled exceptions. Fixes issue 3544
1 parent 2712b93 commit 38e8a90

File tree

2 files changed

+46
-39
lines changed

2 files changed

+46
-39
lines changed

javascript/firefox-driver/js/modals.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -168,30 +168,11 @@ fxdriver.modals.signalOpenModal = function(parent, text) {
168168
if (driver && driver.response_) {
169169
fxdriver.modals.setFlag(driver, text);
170170
var res = driver.response_;
171-
if (driver.response_.name == 'executeAsyncScript' && !driver.response_.responseSent_) {
171+
if (res.name == 'executeAsyncScript' && !res.responseSent_) {
172172
// Special case handling. If a modal is open when executeAsyncScript
173173
// tries to respond with a result, it doesn't do anything, so that this
174174
// codepath can be followed.
175-
fxdriver.modals.isModalPresent(function(present) {
176-
var errorMessage = 'Unexpected modal dialog (text: ' + text + ')';
177-
if (present) {
178-
try {
179-
fxdriver.modals.dismissAlert(driver);
180-
} catch (e) {
181-
errorMessage +=
182-
' The alert could not be closed. The browser may be in a ' +
183-
'wildly inconsistent state, and the alert may still be ' +
184-
'open. This is not good. If you can reliably reproduce this' +
185-
', please report a new issue at ' +
186-
'https://code.google.com/p/selenium/issues/entry ' +
187-
'with reproduction steps. Exception message: ' + e;
188-
}
189-
} else {
190-
errorMessage += ' The alert disappeared before it could be closed.';
191-
}
192-
res.sendError(new WebDriverError(bot.ErrorCode.UNEXPECTED_ALERT_OPEN,
193-
errorMessage, {alert: {text: text}}));
194-
}, 2000);
175+
fxdriver.modals.closeUnhandledAlert(res, driver, false);
195176
} else {
196177
// We hope that modals can only be opened by actions which don't pay
197178
// attention to the results of the command. Given this, the only ways we
@@ -252,3 +233,32 @@ fxdriver.modals.getUnexpectedAlertBehaviour = function() {
252233
var raw = prefs.getCharPref('webdriver_unexpected_alert_behaviour');
253234
return fxdriver.modals.asAcceptableAlertValue(raw);
254235
};
236+
237+
238+
fxdriver.modals.closeUnhandledAlert = function(response, driver, accept) {
239+
var text = driver.modalOpen;
240+
fxdriver.modals.isModalPresent(function(present) {
241+
var errorMessage = 'Unexpected modal dialog (text: ' + text + ')';
242+
if (present) {
243+
try {
244+
if (accept) {
245+
fxdriver.modals.acceptAlert(driver);
246+
} else {
247+
fxdriver.modals.dismissAlert(driver);
248+
}
249+
} catch (e) {
250+
errorMessage +=
251+
' The alert could not be closed. The browser may be in a ' +
252+
'wildly inconsistent state, and the alert may still be ' +
253+
'open. This is not good. If you can reliably reproduce this' +
254+
', please report a new issue at ' +
255+
'https://code.google.com/p/selenium/issues/entry ' +
256+
'with reproduction steps. Exception message: ' + e;
257+
}
258+
} else {
259+
errorMessage += ' The alert disappeared before it could be closed.';
260+
}
261+
response.sendError(new WebDriverError(bot.ErrorCode.UNEXPECTED_ALERT_OPEN,
262+
errorMessage, {alert: {text: text}}));
263+
}, 2000);
264+
};

javascript/firefox-driver/js/nsCommandProcessor.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -481,25 +481,22 @@ nsCommandProcessor.prototype.execute = function(jsonCommandString,
481481
var modalText = driver.modalOpen;
482482
var unexpectedAlertBehaviour = fxdriver.modals.getUnexpectedAlertBehaviour();
483483
switch (unexpectedAlertBehaviour) {
484-
case 'accept':
485-
fxdriver.modals.acceptAlert(driver);
486-
break;
487-
488-
case 'ignore':
489-
// do nothing, ignore the alert
490-
break;
491-
492-
// Dismiss is the default
493-
case 'dismiss':
494-
default:
495-
fxdriver.modals.dismissAlert(driver);
496-
break;
484+
case 'accept':
485+
fxdriver.modals.closeUnhandledAlert(response, driver, true);
486+
break;
487+
488+
case 'ignore':
489+
// do nothing, ignore the alert
490+
response.sendError(new WebDriverError(bot.ErrorCode.UNEXPECTED_ALERT_OPEN,
491+
'Modal dialog present', {alert: {text: modalText}}));
492+
break;
493+
494+
// Dismiss is the default
495+
case 'dismiss':
496+
default:
497+
fxdriver.modals.closeUnhandledAlert(response, driver, false);
498+
break;
497499
}
498-
goog.log.error(nsCommandProcessor.LOG_,
499-
'Sending error from command ' + command.name +
500-
' with alertText: ' + modalText);
501-
response.sendError(new WebDriverError(bot.ErrorCode.UNEXPECTED_ALERT_OPEN,
502-
'Modal dialog present', {alert: {text: modalText}}));
503500
return;
504501
}
505502
}

0 commit comments

Comments
 (0)