deepjavalibrary / deepjavalibrary/djl

Potential Race Condition or Garbage Collection Issue during JNI Environment Usage

Open
#3,010 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.9k
Forks
759
Avg merge
16h 15m
Merged PRs (30d)
15

Description

## Description
The issue arises when a JVM exception is thrown before invoking JNI code, leading to a garbage collection and race condition problem. This results in the `env` variable not existing in the following [line:](https://github.com/deepjavalibrary/djl/blob/master/extensions/tokenizers/rust/src/lib.rs#L440C17-L442C31)

```rust
let obj = env.new_object_unchecked(cls, constructor, &offsets_vec[..]).unwrap();
```

## Expected Behavior

The expected behavior is for the environment object to be properly accessible. It would be nice if an exception were thrown instead of the JVM being crashed.

## Error Message
```
thread '' panicked at src/lib.rs:442:22:
called `Result::unwrap()` on an `Err` value: JavaException
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5
rlwrap: warning: clojure crashed, killed by SIGABRT (core dumped).
rlwrap itself has not crashed, but for transparency,
it will now kill itself with the same signal
```

## How to Reproduce?

The error can most effectively be replicated by employing infinite recursion to generate tokens. I not providing a specific example because we use Clojure, which has too many parentheses to fit into an example.

## Steps to reproduce

Like above.

## What have you tried to solve it?

- Reviewing the rust/JNI code

## Environment Info

Since the issue is related to JNI and JVM interaction, rather than a problem specific to the DJL framework, running ./gradlew debugEnv as suggested might not provide the relevant environmental information needed to debug this issue.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.