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

Fix issue with JNI Env not available #177

Closed
wants to merge 1 commit into from

Conversation

raedle
Copy link
Contributor

@raedle raedle commented 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 : #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

Before

u2net_crashes.mp4

After

u2net_works.mp4

Differential Revision: D41919866

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
@vercel
Copy link

vercel bot commented Dec 12, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated
playtorch ⬜️ Ignored (Inspect) Dec 12, 2022 at 1:18AM (UTC)

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported labels Dec 12, 2022
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D41919866

@facebook-github-bot
Copy link
Contributor

This pull request has been merged in 623f0f5.

raedle added a commit that referenced this pull request 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
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants