★ wanayoo — archive 1999 https://github.com/NativeScript/android-runtime/issues/1602Nouvelle recherche | Portail wanayoo
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

worker crash on physical device with Android 9 or 10 since tns-android 6.4.x #1602

Open
felixkrautschuk opened this issue Apr 14, 2020 · 26 comments
Assignees

Comments

@felixkrautschuk
Copy link

@felixkrautschuk felixkrautschuk commented Apr 14, 2020 •

Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

  • CLI: 6.5.1
  • Cross-platform modules: 6.5.0
  • Android Runtime: 6.5.0 (also reproducable on 6.4.1)
  • iOS Runtime (if applicable): -
  • Plugin(s): nativescript-dev-webpack 1.5.1

Describe the bug
When starting a worker script for the second time on an Androd 9 / 10 physical device, the worker thread throws an exception and most times, the app crashes as well.

Please note

  • it seems to be reproducable only on a physical device with Android 9 or 10, it does not occur on any Android emulator and on device with older Android version
  • everything was working as expected using tns-android 6.3.1 (issue occurs after upgrading to 6.4.1 or 6.5.0) --> update: 6.4.0 is the latest android runtime version without that bug!
  • we reproduce this issue on our Google Pixel 3a (Android 10) and on our Nokia 5.1 (Android 9)
  • the issue does not occur on our Nexus 4 (Android 5.1), Moto X Play (Android 7.1) and Moto Z Play (Android 8.0)

What we are doing in the demo app provided below
On button tap we call this:

const WorkerScript = require("nativescript-worker-loader!./myworker");
const worker = new WorkerScript();

worker.onmessage = function(msg) {
        if(msg.data.isWorkerLoaded) {
            console.log("#### worker notify state 'loaded' -> start action...");

            worker.postMessage({
                action: "test"
            });
        } else if(msg.data.hasOwnProperty('success')) {
            console.log("#### worker success");

            worker.terminate();

            alert("Worker success");
        }
};

worker.onerror = function(e) {
        console.log("#### worker onerror: ", e);
        worker.terminate();

        alert("Worker onerror");
};

And that's the worker script:

//myworker.js

require("@nativescript/core/globals");

//notify main-thread about worker being loaded
global.postMessage({isWorkerLoaded: true});


global.onmessage = function(msg) {
    if(msg.data.action =/old?u=https%3A%2F%2Fgithub.com%2FNativeScript%2Fandroid-runtime%2Fissues%2F%3D%3D&y=1999 "test") {
        setTimeout(function() {
            global.postMessage({ success: true });
        }, 3000);
    }
};

To Reproduce
1.) Start the demo app provided below (on Android 9 or 10 physical device)
2.) Tap the button, wait some short time and you see the alert "Worker success"
3.) Tapping the button for a second time should also alert "Worker success", but instead it crashes the app

4.)

tns platform remove android
tns platform add android@6.4.0
tns run android

5.) repeat steps 1-3 and you see that the app won't crash anymore.

Expected behavior
You should be able to start and terminate this worker script as often as you want, like it was working until tns-android 6.3.1

Sample project
ns-worker-issue-android10.zip

@felixkrautschuk
Copy link
Author

@felixkrautschuk felixkrautschuk commented Apr 14, 2020

In our real app, we do some file-system stuff in the worker script and instead of just silently crashing, the app also logs something like this in the console:

worker onerror: {
JS: "message": "Uncaught TypeError: com.tns.NativeScriptApplication.getInstance is not a function",
JS: "stackTrace": "TypeError: com.tns.NativeScriptApplication.getInstance is not a function\n at Object.getNativeApplication (file:///data/data/org.nativescript.nsworkerissueandroid10/files/app/c9e203512527157969ea.worker.js:502:57)\n at getApplicationContext (file:///data/data/org.nativescript.nsworkerissueandroid10/files/app/c9e203512527157969ea.worker.js:4832:44)\n at FileSystemAccess.../node_modules/@nativescript/core/file-system/file-system-access.js.FileSystemAccess.getDocumentsFolderPath (file:///data/data/org.nativescript.nsworkerissueandroid10/files/app/c9e203512527157969ea.worker.js:4991:19)\n at Object.documents (file:///data/data/org.nativescript.nsworkerissueandroid10/files/app/c9e203512527157969ea.worker.js:5778:42)\n at ../node_modules/nativescript-dev-webpack/hmr/hot-loader.js!./myworker.js.global.onmessage (file:///data/data/org.nativescript.nsworkerissueandro...

I updated the demo app to do some totally simplified file system stuff:

//myworker.js

global.onmessage = function(msg) {
    if(msg.data.action =/old?u=https%3A%2F%2Fgithub.com%2FNativeScript%2Fandroid-runtime%2Fissues%2F%3D%3D&y=1999 "test") {
        const documents = fileSystemModule.knownFolders.documents();
        const folderCustom = documents.getFolder("custom");
        var pathToFile = fileSystemModule.path.join(folderCustom.path, "test" + ".json");

        const json = {
            "id": 1,
            "foo": "bar"
        };

        fileSystemModule.File.fromPath(pathToFile).writeTextSync(JSON.stringify(json));

        global.postMessage({ success: true });
    }
};


global.onerror = function(err) {
    console.log("global.onerror:");
    console.log(err);
};

ns-worker-issue-android10.zip

@NathanaelA
Copy link
Member

@NathanaelA NathanaelA commented Apr 14, 2020

@felixkrautschuk - Does your app use Flavors in your gradle configurations by any chance? I worked with #1597 and we spent a lot of time tracking it down to flavors messing something up, but your error I believe is identical, and it was with workers...

@felixkrautschuk
Copy link
Author

@felixkrautschuk felixkrautschuk commented Apr 14, 2020

@NathanaelA no it does not use flavors, neither the demo app from here nor our real app.

@NathanaelA
Copy link
Member

@NathanaelA NathanaelA commented Apr 14, 2020

@felixkrautschuk - Thanks for confirming that -- I was really hoping you would say yes, but guessed that it would be no since you said you could duplicate it with your demo. :-)

@felixkrautschuk
Copy link
Author

@felixkrautschuk felixkrautschuk commented Apr 14, 2020

Sorry for that :D

When I run into a new issue, it is always the first step for me to create a new app project and check if it also occurs there and if not, I know that is must be a mistake from myself in my own project.
But that's not the case here ;-)

@felixkrautschuk felixkrautschuk changed the title worker crash on Android 10 physical device since tns-android 6.4.x worker crash on physical device with Android 9 or 10 since tns-android 6.4.x Apr 16, 2020
@felixkrautschuk
Copy link
Author

@felixkrautschuk felixkrautschuk commented Apr 16, 2020

Just few more information on this:

The issue is not related on Android 10 only and also occurs on Android 9 physical devices. On devices with older Android versions, we cannot reproduce this (I already updated the title and description of this thread).

If the worker only sends a message back to the main thread (or may be does a console log before that) and then terminates, everything works as expected on ALL devices.

But as soon as you but that into a setTimeout-call, it crashes with

JS: worker onerror: {
JS: "message": "Uncaught TypeError: android.os.Looper.myLooper is not a function",
JS: "stackTrace": "TypeError: android.os.Looper.myLooper is not a function\n at createHandlerAndGetId (file:///data/data/org.nativescript.nsworkerissueandroid10/files/app/0f9e8cc21cc309f62b64.worker.js:10892:67)\n at setTimeout (file:///data/data/org.nativescript.nsworkerissueandroid10/files/app/0f9e8cc21cc309f62b64.worker.js:10904:14)\n at ../node_modules/nativescript-dev-webpack/hmr/hot-loader.js!./myworker.js.global.onmessage (file: app/myworker.js:36:0)",
JS: "filename": "file:///data/data/org.nativescript.nsworkerissueandroid10/files/app/0f9e8cc21cc309f62b64.worker.js",
JS: "lineno": 10892
JS: }

@NathanaelA
Copy link
Member

@NathanaelA NathanaelA commented Apr 16, 2020

@felixkrautschuk - One minor question; you say it fails on 6.4.1 and 6.5.0 does this work fine on 6.4.0?

@felixkrautschuk
Copy link
Author

@felixkrautschuk felixkrautschuk commented Apr 17, 2020

@NathanaelA Indeed, 6.4.0 is the latest working android runtime, not 6.3.1.

In the release notes, they only mentioned 6.4.1, that's why I did not explicitly test 6.4.0.

@saschaarthur
Copy link
Contributor

@saschaarthur saschaarthur commented Apr 26, 2020

any idears/updates to this?

@vmutafov vmutafov self-assigned this May 19, 2020
@alexander-mai
Copy link

@alexander-mai alexander-mai commented Jun 12, 2020

I have the same problem in my app.
If I don't terminate the Worker process, the problem is gone. Is it possible, that the application is trying to reuse the terminated worker instance?

@felixkrautschuk
Copy link
Author

@felixkrautschuk felixkrautschuk commented Jun 14, 2020

I just found out that it is not related only on tns-android 6.4.1+, it also happens on tns-android 6.4.0, but not that often like on 6.5.0 or 6.5.1.

It also happens on Emulators by the way... not sure why it was working on emulators for me when I created this issue.

@lochstar
Copy link

@lochstar lochstar commented Jun 16, 2020

I'm having the same problem. Currently on tns-android 6.5.0

@NathanaelA NathanaelA closed this Jul 8, 2020
@NathanaelA NathanaelA reopened this Jul 8, 2020
@phdd
Copy link

@phdd phdd commented Jul 28, 2020

Confirmed on tns-android 6.5.3

@Pkurto
Copy link

@Pkurto Pkurto commented Jul 28, 2020

Same problem in my app: tns android 6.5.3 and physical devices android 10

@NathanaelA
Copy link
Member

@NathanaelA NathanaelA commented Jul 28, 2020 •

Just a status update, I finally had a chance with the above demos I am able to cause a random crash on both the android.os.looper and it totally segfaulted on an emulator a couple of times. So I can duplicate this on Android 10 emulators with both samples.. THANK YOU @felixkrautschuk for the samples!


It does look like something in the worker is getting corrupted; not sure what yet.


After some more discussions with my partners; we have agreed that this type of bug should be fixed on a higher priority basis, as crashing is a bad thing. So I'll be looking into it shortly. Hopefully have some good news soon.

@NathanaelA NathanaelA assigned NathanaelA and unassigned vmutafov Jul 28, 2020
@senner007
Copy link

@senner007 senner007 commented Aug 25, 2020

I have the same issue. The app crashes if I call .terminate() or close()

@DeclanKillilea
Copy link

@DeclanKillilea DeclanKillilea commented Sep 1, 2020

Hi - Is there a release version date / version chosen for this fix as yet? Thanks

@NathanaelA
Copy link
Member

@NathanaelA NathanaelA commented Sep 1, 2020

Hi, this is actually one of my higher priority bugs I'm looking into -- now that we finally pushed out NS7 yesterday; the availability to track down some of these bugs should be a lot more available. :) I will be converting the two sample app to NS7 today and verifying that the issue wasn't fixed by some of the other issues we fixed in NS7 and/or by the engine upgrade that also happened.

@DeclanKillilea
Copy link

@DeclanKillilea DeclanKillilea commented Sep 1, 2020

Hi, this is actually one of my higher priority bugs I'm looking into -- now that we finally pushed out NS7 yesterday; the availability to track down some of these bugs should be a lot more available. :) I will be converting the two sample app to NS7 today and verifying that the issue wasn't fixed by some of the other issues we fixed in NS7 and/or by the engine upgrade that also happened.

Great thanks, I tried running npm view tns-android versions today but its not picked up 7.0.0 as I was hoping to test similar to see if that upgrade has fixed it

@NathanaelA
Copy link
Member

@NathanaelA NathanaelA commented Sep 1, 2020 •

It is no longer called tns-android; everything is now in the @NativeScript namespace. So @nativescript/android is the runtime...

If you want to manually try this engine with a older CLI; you can use npm info to get the download link (or download it from github releases) of the latest engine, then use tns platform add android --frameworkPath <tgz_file_downloaded>

@lostation
Copy link

@lostation lostation commented Sep 11, 2020

Exact same behavior here.... with any call to terminate inside or outside of worker -> crash... without any call to terminate()...its works... @NathanaelA Please if you fin a solution ( soon I hope ;-) )...Can you also make it available for NS 6.8.0... I'm reaaaally not ready to upgrade to NS 7 right now... -> too big angular 8 shared code project...will make it slow to angular 10..

@lostation
Copy link

@lostation lostation commented Sep 11, 2020

Is it a so really bad practice to not call terminate() on worker ref ?
Instead I'm currently set the worker ref to null... and recreate a new worker each time I need it...
It doesn't seems to get any issue until there... Should be garbage collected at time... Am I right ?

@NathanaelA
Copy link
Member

@NathanaelA NathanaelA commented Sep 11, 2020

@lostation - We would make the patch for NS7 Android runtime, since that is what we are maintaining... However, before that statement worries you; the good news is you can easily use the NS7 runtimes with NS6. Their are no breaking changes and they should be 100% compatible. :-)

Now as to how you are causing a crash, is all you are doing is calling terminate() inside the worker? Does it crash every time?

@lostation
Copy link

@lostation lostation commented Sep 11, 2020

@NathanaelA
Copy link
Member

@NathanaelA NathanaelA commented Sep 11, 2020

NS has several pieces;

  • CLI (i.e. tns / nativescript command)
  • Core Modules (node_modules/@NativeScript/core or node_moduiles/tns_core_modules)
  • Android runtime (platforms/android)
  • iOS runtrime (platforms/ios)

The android runtime is where this issue would be occurring, if you are calling terminate and it is crashing.

The NS 7 android runtime should currently be backwards compatible with NS6, however to install it you have manually download it and use the --frameworkPath variable that I described a couple posts up in this issue. because the NS6 CLI tooling is unaware of the @nativescript/android name space.

@benediktveith
Copy link

@benediktveith benediktveith commented Oct 30, 2020

A fix for this bug would be highly appreciated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.