Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

U2Net Model Example Runs Once Successfully, Crashes After #175

Closed
lawlcakez opened this issue Dec 11, 2022 · 3 comments
Closed

U2Net Model Example Runs Once Successfully, Crashes After #175

lawlcakez opened this issue Dec 11, 2022 · 3 comments

Comments

@lawlcakez
Copy link

lawlcakez commented Dec 11, 2022

Version

^0.2.3

Problem Area

react-native-pytorch-core (core package)

Steps to Reproduce

  1. Run u2net model example using example code from https://playtorch.dev/snack/@playtorch/u2net/.
  2. Successfully generates mask on first run, crashes after trying to run past that.

Expected Results

Model should run without crashing after first run.

Crash Logs:

12-10 18:51:24.290 20638 20638 F DEBUG : backtrace:
12-10 18:51:24.290 20638 20638 F DEBUG : #00 pc 0000000000051b20 /apex/com.android.runtime/lib64/bionic/libc.so (abort+168) (BuildId: 73e145ce4e1dfca57a8eaf6d29c1d3fe)
12-10 18:51:24.290 20638 20638 F DEBUG : #1 pc 000000000009ce88 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libc++_shared.so (BuildId: 31abebc1b564add54fe43b52ce69acac326be4fe)
12-10 18:51:24.290 20638 20638 F DEBUG : #2 pc 000000000009d07c /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libc++_shared.so (BuildId: 31abebc1b564add54fe43b52ce69acac326be4fe)
12-10 18:51:24.290 20638 20638 F DEBUG : #3 pc 00000000000aead0 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libc++_shared.so (BuildId: 31abebc1b564add54fe43b52ce69acac326be4fe)
12-10 18:51:24.290 20638 20638 F DEBUG : #4 pc 00000000000aea68 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libc++_shared.so (std::terminate()+36) (BuildId: 31abebc1b564add54fe43b52ce69acac326be4fe)
12-10 18:51:24.290 20638 20638 F DEBUG : #5 pc 000000000003cff0 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libtorchlive.so (__clang_call_terminate+8) (BuildId: 0a803d5f698a12e07d4c31b32095ee75a8eefd87)
12-10 18:51:24.290 20638 20638 F DEBUG : #6 pc 0000000000088f00 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libtorchlive.so (torchlive::media::Image::~Image()+92) (BuildId: 0a803d5f698a12e07d4c31b32095ee75a8eefd87)
12-10 18:51:24.290 20638 20638 F DEBUG : #7 pc 0000000000043874 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libtorchlive.so (torchlive::media::ImageHostObject::~ImageHostObject()+76) (BuildId: 0a803d5f698a12e07d4c31b32095ee75a8eefd87)
12-10 18:51:24.290 20638 20638 F DEBUG : #8 pc 000000000001cc20 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libhermes-executor-release.so (facebook::jsi::DecoratedHostObject::~DecoratedHostObject()+76) (BuildId: f9ec095fd26bd03ab3b56d56ec655670c546c472)
12-10 18:51:24.290 20638 20638 F DEBUG : #9 pc 000000000007033c /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libhermes.so (BuildId: 957ec0f39e08feadc6d0ec99bbe00fa14a3687b3)
12-10 18:51:24.290 20638 20638 F DEBUG : #10 pc 00000000000798fc /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libhermes.so (BuildId: 957ec0f39e08feadc6d0ec99bbe00fa14a3687b3)
12-10 18:51:24.290 20638 20638 F DEBUG : #11 pc 0000000000131190 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libhermes.so (BuildId: 957ec0f39e08feadc6d0ec99bbe00fa14a3687b3)
12-10 18:51:24.290 20638 20638 F DEBUG : #12 pc 000000000013326c /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libhermes.so (BuildId: 957ec0f39e08feadc6d0ec99bbe00fa14a3687b3)
12-10 18:51:24.290 20638 20638 F DEBUG : #13 pc 0000000000138d38 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libhermes.so (BuildId: 957ec0f39e08feadc6d0ec99bbe00fa14a3687b3)
12-10 18:51:24.290 20638 20638 F DEBUG : #14 pc 0000000000137a54 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libhermes.so (BuildId: 957ec0f39e08feadc6d0ec99bbe00fa14a3687b3)
12-10 18:51:24.290 20638 20638 F DEBUG : #15 pc 000000000013861c /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libhermes.so (BuildId: 957ec0f39e08feadc6d0ec99bbe00fa14a3687b3)
12-10 18:51:24.290 20638 20638 F DEBUG : #16 pc 00000000001384b4 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libhermes.so (BuildId: 957ec0f39e08feadc6d0ec99bbe00fa14a3687b3)
12-10 18:51:24.290 20638 20638 F DEBUG : #17 pc 00000000000b6e44 /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start(void*)+264) (BuildId: 73e145ce4e1dfca57a8eaf6d29c1d3fe)
12-10 18:51:24.290 20638 20638 F DEBUG : #18 pc 0000000000053454 /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+68) (BuildId: 73e145ce4e1dfca57a8eaf6d29c1d3fe)
12-10 19:15:44.434 21437 21567 F libc : Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 21567 (hades), pid 21437 (.faceapp_native)
12-10 19:15:45.014 26451 26451 F DEBUG : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
12-10 19:15:45.014 26451 26451 F DEBUG : Build fingerprint: 'samsung/d2xq2sq/d2xq2:12/SP1A.210812.016/N976USQS5GVJ2:user/release-keys'
12-10 19:15:45.014 26451 26451 F DEBUG : Revision: '18'
12-10 19:15:45.014 26451 26451 F DEBUG : ABI: 'arm64'
12-10 19:15:45.014 26451 26451 F DEBUG : Processor: '5'
12-10 19:15:45.014 26451 26451 F DEBUG : Timestamp: 2022-12-10 19:15:44.601109083-0500
12-10 19:15:45.014 26451 26451 F DEBUG : Process uptime: 972s
12-10 19:15:45.014 26451 26451 F DEBUG : Cmdline: com.faceapp_native
12-10 19:15:45.014 26451 26451 F DEBUG : pid: 21437, tid: 21567, name: hades >>> com.faceapp_native <<<
12-10 19:15:45.014 26451 26451 F DEBUG : uid: 10751
12-10 19:15:45.014 26451 26451 F DEBUG : signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr --------
12-10 19:15:45.014 26451 26451 F DEBUG : Abort message: 'terminating with uncaught exception of type std::runtime_error: Unable to retrieve jni environment. Is the thread attached?'
12-10 19:15:45.014 26451 26451 F DEBUG : x0 0000000000000000 x1 000000000000543f x2 0000000000000006 x3 0000006fb83fb050
12-10 19:15:45.014 26451 26451 F DEBUG : x4 736f646277641f73 x5 736f646277641f73 x6 736f646277641f73 x7 7f7f7f7f7f7f7f7f
12-10 19:15:45.014 26451 26451 F DEBUG : x8 00000000000000f0 x9 84d37911297e4f60 x10 0000000000000000 x11 ffffff80fffffbdf
12-10 19:15:45.014 26451 26451 F DEBUG : x12 0000000000000001 x13 0000000063952130 x14 0018fcfca9eed265 x15 0000db9016d3ed9a
12-10 19:15:45.014 26451 26451 F DEBUG : x16 000000734fdc4060 x17 000000734fda0980 x18 0000006ef44fa000 x19 00000000000053bd
12-10 19:15:45.014 26451 26451 F DEBUG : x20 000000000000543f x21 00000000ffffffff x22 ffffff80ffffffc8 x23 0000006fb83fb2a0
12-10 19:15:45.014 26451 26451 F DEBUG : x24 0000006fb83fb180 x25 0000006fb83fb1c0 x26 0000006ef7400000 x27 0000000000000001
12-10 19:15:45.014 26451 26451 F DEBUG : x28 0000006fde5b0fd0 x29 0000006fb83fb0d0
12-10 19:15:45.014 26451 26451 F DEBUG : lr 000000734fd50af4 sp 0000006fb83fb030 pc 000000734fd50b20 pst 0000000000000000
12-10 19:15:45.014 26451 26451 F DEBUG : backtrace:
12-10 19:15:45.014 26451 26451 F DEBUG : #00 pc 0000000000051b20 /apex/com.android.runtime/lib64/bionic/libc.so (abort+168) (BuildId: 73e145ce4e1dfca57a8eaf6d29c1d3fe)
12-10 19:15:45.014 26451 26451 F DEBUG : #1 pc 000000000009ce88 /data/app/~~oaswytM_7c70c2x3P691BQ==/com.faceapp_native-jBo2sa_SNO0cIvjju4fygw==/lib/arm64/libc++_shared.so (BuildId: 31abebc1b564add54fe43b52ce69acac326be4fe)
12-10 19:15:45.014 26451 26451 F DEBUG : #2 pc 000000000009d07c /data/app/~~oaswytM_7c70c2x3P691BQ==/com.faceapp_native-jBo2sa_SNO0cIvjju4fygw==/lib/arm64/libc++_shared.so (BuildId: 31abebc1b564add54fe43b52ce69acac326be4fe)
12-10 19:15:45.014 26451 26451 F DEBUG : #3 pc 00000000000aead0 /data/app/~~oaswytM_7c70c2x3P691BQ==/com.faceapp_native-jBo2sa_SNO0cIvjju4fygw==/lib/arm64/libc++_shared.so (BuildId: 31abebc1b564add54fe43b52ce69acac326be4fe)
12-10 19:15:45.014 26451 26451 F DEBUG : #4 pc 00000000000aea68 /data/app/~~oaswytM_7c70c2x3P691BQ==/com.faceapp_native-jBo2sa_SNO0cIvjju4fygw==/lib/arm64/libc++_shared.so (std::terminate()+36) (BuildId: 31abebc1b564add54fe43b52ce69acac326be4fe)
12-10 19:15:45.014 26451 26451 F DEBUG : #5 pc 00000000000f5944 /data/app/~~oaswytM_7c70c2x3P691BQ==/com.faceapp_native-jBo2sa_SNO0cIvjju4fygw==/lib/arm64/libtorchlive.so (BuildId: d34f8f8b13b73951fb8c4c9ffe73069a41558283)

Code example, screenshot, or link to repository

No response

raedle added a commit to raedle/playtorch that referenced this issue Dec 12, 2022
Summary:
The Hermes GC will eventually clean up orphan host objects. This is also the case for `ImageHostObject`, which itself has a reference to `Image` that needs to be cleaned up, hence the call to the `Image::~Image` destructor. On Android, it will try to release the `global_ref<JImage>` resource, which at the time, may not run in the JVM thread scope causing an app carsh with an error like:

```
Abort message: 'terminating with uncaught exception of type std::runtime_error: Unable to retrieve jni environment. Is the thread attached?'
```
```
/data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libtorchlive.so (__clang_call_terminate+8) (BuildId: 0a803d5f698a12e07d4c31b32095ee75a8eefd87)
12-10 18:51:24.290 20638 20638 F DEBUG : facebookresearch#6 pc 0000000000088f00 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libtorchlive.so (torchlive::media::Image::~Image()+92) (BuildId: 0a803d5f698a12e07d4c31b32095ee75a8eefd87)
12-10 18:51:24.290 20638 20638 F DEBUG : facebookresearch#7 pc 0000000000043874 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libtorchlive.so (torchlive::media::ImageHostObject::~ImageHostObject()+76) (BuildId: 0a803d5f698a12e07d4c31b32095ee75a8eefd87)
12-10 18:51:24.290 20638 20638 F DEBUG : facebookresearch#8 pc 000000000001cc20 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libhermes-executor-release.so (facebook::jsi::DecoratedHostObject::~DecoratedHostObject()+76) (BuildId: f9ec095fd26bd03ab3b56d56ec655670c546c472)
12-10 18:51:24.290 20638 20638 F DEBUG : facebookresearch#9 pc 000000000007033c /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libhermes.so (BuildId: 957ec0f39e08feadc6d0ec99bbe00fa14a3687b3)
```

Reported here: facebookresearch#175

This change ensures that the image instance release happens within the JVM environment.

More details: https://github.com/facebookincubator/fbjni/blob/5eacdd11c5d9c46f5a752faad3457f3b07fe40cb/cxx/fbjni/detail/Environment.h#L114

Differential Revision: D41919866

fbshipit-source-id: 6b0fdb4b9719988429ffc5376e4ba8349850c903
@raedle
Copy link
Contributor

raedle commented Dec 12, 2022

Thanks for reporting this issue, @lawlcakez! We are able to reproduce the issue and submitted a potential fix in #177 (see before/after screencasts below).

The fix will eventually be available in the next PlayTorch app release v0.1.3.

If you need the fix faster, it will be available in a react-native-pytorch-core nightly build after the fix merged. Then, there are two options:

  1. Build the PlayTorch app locally. I submitted a PR with basic instructions to build the app for Android and iOS (Basic instructions to build the PlayTorch app #178). It'll require to upgrade the react-native-pytorch-core version in the app/package.json from 0.2.3 to the nightly version.
  2. Build a custom Expo app (see instructions Expo App in the PlayTorch Get Started section)

I will close the issue. Feel free to reopen the issue if there issue isn't solved with #177

Before

u2net_crashes.mp4

After

u2net_works.mp4

@raedle raedle closed this as completed Dec 12, 2022
facebook-github-bot pushed a commit that referenced this issue Dec 12, 2022
Summary:
Pull Request resolved: #177

The Hermes GC will eventually clean up orphan host objects. This is also the case for `ImageHostObject`, which itself has a reference to `Image` that needs to be cleaned up, hence the call to the `Image::~Image` destructor. On Android, it will try to release the `global_ref<JImage>` resource, which at the time, may not run in the JVM thread scope causing an app carsh with an error like:

```
Abort message: 'terminating with uncaught exception of type std::runtime_error: Unable to retrieve jni environment. Is the thread attached?'
```
```
/data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libtorchlive.so (__clang_call_terminate+8) (BuildId: 0a803d5f698a12e07d4c31b32095ee75a8eefd87)
12-10 18:51:24.290 20638 20638 F DEBUG : #6 pc 0000000000088f00 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libtorchlive.so (torchlive::media::Image::~Image()+92) (BuildId: 0a803d5f698a12e07d4c31b32095ee75a8eefd87)
12-10 18:51:24.290 20638 20638 F DEBUG : #7 pc 0000000000043874 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libtorchlive.so (torchlive::media::ImageHostObject::~ImageHostObject()+76) (BuildId: 0a803d5f698a12e07d4c31b32095ee75a8eefd87)
12-10 18:51:24.290 20638 20638 F DEBUG : #8 pc 000000000001cc20 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libhermes-executor-release.so (facebook::jsi::DecoratedHostObject::~DecoratedHostObject()+76) (BuildId: f9ec095fd26bd03ab3b56d56ec655670c546c472)
12-10 18:51:24.290 20638 20638 F DEBUG : #9 pc 000000000007033c /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libhermes.so (BuildId: 957ec0f39e08feadc6d0ec99bbe00fa14a3687b3)
```

Reported here: #175

This change ensures that the image instance release happens within the JVM environment.

More details: https://github.com/facebookincubator/fbjni/blob/5eacdd11c5d9c46f5a752faad3457f3b07fe40cb/cxx/fbjni/detail/Environment.h#L114

Reviewed By: ansonsyfang

Differential Revision: D41919866

fbshipit-source-id: 103fb2d5bdadf42e8d79882874147abe58d3a1c2
@raedle
Copy link
Contributor

raedle commented Dec 12, 2022

@lawlcakez, we landed a fix, which is available in the latest react-native-pytorch-core@nighly

@lawlcakez
Copy link
Author

@raedle, thanks! Just implemented the nightly fix and can confirm the crash no longer happens on my native app.

raedle added a commit that referenced this issue Dec 17, 2022
Summary:
Pull Request resolved: #177

The Hermes GC will eventually clean up orphan host objects. This is also the case for `ImageHostObject`, which itself has a reference to `Image` that needs to be cleaned up, hence the call to the `Image::~Image` destructor. On Android, it will try to release the `global_ref<JImage>` resource, which at the time, may not run in the JVM thread scope causing an app carsh with an error like:

```
Abort message: 'terminating with uncaught exception of type std::runtime_error: Unable to retrieve jni environment. Is the thread attached?'
```
```
/data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libtorchlive.so (__clang_call_terminate+8) (BuildId: 0a803d5f698a12e07d4c31b32095ee75a8eefd87)
12-10 18:51:24.290 20638 20638 F DEBUG : #6 pc 0000000000088f00 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libtorchlive.so (torchlive::media::Image::~Image()+92) (BuildId: 0a803d5f698a12e07d4c31b32095ee75a8eefd87)
12-10 18:51:24.290 20638 20638 F DEBUG : #7 pc 0000000000043874 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libtorchlive.so (torchlive::media::ImageHostObject::~ImageHostObject()+76) (BuildId: 0a803d5f698a12e07d4c31b32095ee75a8eefd87)
12-10 18:51:24.290 20638 20638 F DEBUG : #8 pc 000000000001cc20 /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libhermes-executor-release.so (facebook::jsi::DecoratedHostObject::~DecoratedHostObject()+76) (BuildId: f9ec095fd26bd03ab3b56d56ec655670c546c472)
12-10 18:51:24.290 20638 20638 F DEBUG : #9 pc 000000000007033c /data/app/~~tGo_W1LgnykvwiT9t7jBtQ==/dev.playtorch-NUv9YPDoqWoQiv-dKR2ylA==/lib/arm64/libhermes.so (BuildId: 957ec0f39e08feadc6d0ec99bbe00fa14a3687b3)
```

Reported here: #175

This change ensures that the image instance release happens within the JVM environment.

More details: https://github.com/facebookincubator/fbjni/blob/5eacdd11c5d9c46f5a752faad3457f3b07fe40cb/cxx/fbjni/detail/Environment.h#L114

Reviewed By: ansonsyfang

Differential Revision: D41919866

fbshipit-source-id: 103fb2d5bdadf42e8d79882874147abe58d3a1c2
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants