Uploaded image for project: 'Apache Cordova'
  1. Apache Cordova
  2. CB-8468

Application freezes if breakpoint hits JavaScript callback invoked from native

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • 4.0.0
    • cordova-ios, cordova-js
    • None

    Description

      It you attach Safari to your application and hit a breakpoint in a Cordova callback the application freezes and you can't step through or continue. This only happens in breakpoints in the native to JavaScript callbacks. JavaScript breakpoints in other areas work fine.

      A simple way to reproduce is to use the globalization plugin. Attach safari and put a breakpoint in the success callback.

      navigator.globalization.getPreferredLanguage(function() {
              console.log("test"); // Will freeze if breakpoint set here
          }, function() {});
      

      The problem seems to have to do with the webview stringByEvaluatingJavaScriptFromString. It is a synchronous call that can return the result of the JavaScript back to the native. The native code will crash if a breakpoint is hit while trying to execute the JavaScript. The workaround is to have your callback code in a setTimeout so it is not in the same context as the native stringByEvaluatingJavaScriptFromString call.

      A workaround:

      Open cordova.js and around line 281 replace:

      callbackFromNative: function(callbackId, isSuccess, status, args, keepCallback) {
       

      With:

          callbackFromNative: function(callbackId, isSuccess, status, args, keepCallback) {
             setTimeout(function() {
                  cordova.callbackFromNative2(callbackId, isSuccess, status, args, keepCallback);
              }, 0);
          },
             
          callbackFromNative2: function(callbackId, isSuccess, status, args, keepCallback) {
      

      Attachments

        Issue Links

          Activity

            People

              shazron Shazron Abdullah
              mpridham Marcus Pridham
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: