Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions in event handlers breaks further processing of event handlers #3580

Merged
merged 3 commits into from
Aug 25, 2016

Conversation

imbcmdth
Copy link
Member

@imbcmdth imbcmdth commented Aug 25, 2016

Description

In the event dispatch function, we do not guard against event handlers raising exceptions. If that happens, any further processing of event handlers and any code after the trigger stops as the exception continues to unwind the stack.

This is particularly problematic if the event was triggered by a source handler during the initialization stages of the handler. The SourceHandler can end up in a state from which it can not recover through no fault of our own.

See this example of a player broken by a simple exception thrown from an event handler:

http://jsbin.com/fipikerore/edit?html,output

Specific Changes proposed

Simply add a try/catch around each invocation of an event handler function called from the the dispatch function.

Requirements Checklist

  • Feature implemented / Bug fixed
  • If necessary, more likely in a feature request than a bug fix
    • Unit Tests updated or fixed
    • Docs/guides updated
    • Example created
  • Reviewed by Two Core Contributors

@@ -248,7 +249,11 @@ export function on(elem, type, fn) {
if (event.isImmediatePropagationStopped()) {
break;
} else {
handlersCopy[m].call(elem, event, hash);
try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this try/catch should probably be wrapped in an IIFE because try/catch

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make the code a lot uglier than it already is for little to no benefit. There should never be so many event handlers for any single event that the performance difference becomes an issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I lean towards leaving this as-is.

@imbcmdth imbcmdth changed the base branch from master to stable August 25, 2016 22:31
@imbcmdth imbcmdth force-pushed the safe-event-handlers branch from 1b0990a to 5373966 Compare August 25, 2016 22:35

const el = document.createElement('div');
const listener1 = function() {
throw new Error('GURU MEDITATION ERROR');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet.

@dmlap
Copy link
Member

dmlap commented Aug 25, 2016

LGTM

@imbcmdth imbcmdth merged commit fdd8550 into videojs:stable Aug 25, 2016
@imbcmdth imbcmdth deleted the safe-event-handlers branch August 25, 2016 23:08
helenjer pushed a commit to helenjer/video.js that referenced this pull request Sep 6, 2016
…s#3580

* Guard against exceptions in an event handler to stop them from breaking further processing of event handlers

* Added a test for try/catch behavior for exceptions originating in event handlers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants