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

JavaKit: Passing objects between threads #157

Open
lhoward opened this issue Nov 7, 2024 · 2 comments
Open

JavaKit: Passing objects between threads #157

lhoward opened this issue Nov 7, 2024 · 2 comments
Labels

Comments

@lhoward
Copy link
Contributor

lhoward commented Nov 7, 2024

javaMethodLookup() uses the environment attached to the object, probably it should use the ambient (thread-specific) environment or the one that is passed in?

java_vm_ext.cc:599] JNI DETECTED ERROR IN APPLICATION: thread Thread[23,tid=1783,Native,Thread*=0xb40000718be7eb40,peer=0x1449d000,"Thread-4"] using JNIEnv* from thread Thread[1,tid=1631,Native,Thread*=0xb40000718be307b0,peer=0x72c1eb68,"main"]
                                                                                                    java_vm_ext.cc:599]     in call to GetObjectClass

From the JNI specs:

The JNI interface pointer is only valid in the current thread. A native method, therefore, must not pass the interface pointer from one thread to another. A VM implementing the JNI may allocate and store thread-local data in the area pointed to by the JNI interface pointer.

So, I think this is a bug. Is the fix as simple as adding an environment argument to javaMethodLookup()?

@lhoward
Copy link
Contributor Author

lhoward commented Nov 7, 2024

It's not a huge issue because I can schedule things on the main thread but, would be nice to not need to do this (to avoid excess context switches).

@ktoso ktoso changed the title Passing objects between threads JavaKit: Passing objects between threads Nov 7, 2024
@ktoso ktoso added the javakit label Nov 7, 2024
lhoward added a commit to PADL/swift-java that referenced this issue Nov 7, 2024
lhoward added a commit to PADL/swift-java that referenced this issue Nov 7, 2024
This is not a real fix, I suspect the real fix is to remove javaEnvironment as
an instance variable on all platforms and always use the ambient environment
(as the JNI specification clearly states the environment cannot be shared
between threads).

Works around: swiftlang#157
@lhoward
Copy link
Contributor Author

lhoward commented Nov 8, 2024

I just did this for now – PADL@c3f2314 – but obviously it's a workaround. I think I need upstream input as to what the right solution is, whether we remove javaEnvironment completely and always use the ambient environment. cc @DougGregor

lhoward added a commit to PADL/swift-java that referenced this issue Nov 8, 2024
This is not a real fix, I suspect the real fix is to remove javaEnvironment as
an instance variable on all platforms and always use the ambient environment
(as the JNI specification clearly states the environment cannot be shared
between threads).

Works around: swiftlang#157
lhoward added a commit to PADL/swift-java that referenced this issue Nov 8, 2024
This is not a real fix, I suspect the real fix is to remove javaEnvironment as
an instance variable on all platforms and always use the ambient environment
(as the JNI specification clearly states the environment cannot be shared
between threads).

Works around: swiftlang#157
lhoward added a commit to PADL/swift-java that referenced this issue Nov 8, 2024
This is not a real fix, I suspect the real fix is to remove javaEnvironment as
an instance variable on all platforms and always use the ambient environment
(as the JNI specification clearly states the environment cannot be shared
between threads).

Works around: swiftlang#157
lhoward added a commit to PADL/swift-java that referenced this issue Nov 8, 2024
This is not a real fix, I suspect the real fix is to remove javaEnvironment as
an instance variable on all platforms and always use the ambient environment
(as the JNI specification clearly states the environment cannot be shared
between threads).

Works around: swiftlang#157
lhoward added a commit to PADL/swift-java that referenced this issue Nov 8, 2024
This is not a real fix, I suspect the real fix is to remove javaEnvironment as
an instance variable on all platforms and always use the ambient environment
(as the JNI specification clearly states the environment cannot be shared
between threads).

Works around: swiftlang#157
lhoward added a commit to PADL/swift-java that referenced this issue Nov 8, 2024
This is not a real fix, I suspect the real fix is to remove javaEnvironment as
an instance variable on all platforms and always use the ambient environment
(as the JNI specification clearly states the environment cannot be shared
between threads).

Works around: swiftlang#157
lhoward added a commit to PADL/swift-java that referenced this issue Nov 9, 2024
This is not a real fix, I suspect the real fix is to remove javaEnvironment as
an instance variable on all platforms and always use the ambient environment
(as the JNI specification clearly states the environment cannot be shared
between threads).

Works around: swiftlang#157
lhoward added a commit to PADL/swift-java that referenced this issue Nov 9, 2024
This is not a real fix, I suspect the real fix is to remove javaEnvironment as
an instance variable on all platforms and always use the ambient environment
(as the JNI specification clearly states the environment cannot be shared
between threads).

Works around: swiftlang#157
lhoward added a commit to PADL/swift-java that referenced this issue Nov 9, 2024
This is not a real fix, I suspect the real fix is to remove javaEnvironment as
an instance variable on all platforms and always use the ambient environment
(as the JNI specification clearly states the environment cannot be shared
between threads).

Works around: swiftlang#157
lhoward added a commit to PADL/swift-java that referenced this issue Nov 9, 2024
This is not a real fix, I suspect the real fix is to remove javaEnvironment as
an instance variable on all platforms and always use the ambient environment
(as the JNI specification clearly states the environment cannot be shared
between threads).

Works around: swiftlang#157
lhoward added a commit to PADL/swift-java that referenced this issue Nov 9, 2024
This is not a real fix, I suspect the real fix is to remove javaEnvironment as
an instance variable on all platforms and always use the ambient environment
(as the JNI specification clearly states the environment cannot be shared
between threads).

Works around: swiftlang#157
lhoward added a commit to PADL/swift-java that referenced this issue Nov 9, 2024
This is not a real fix, I suspect the real fix is to remove javaEnvironment as
an instance variable on all platforms and always use the ambient environment
(as the JNI specification clearly states the environment cannot be shared
between threads).

Works around: swiftlang#157
lhoward added a commit to PADL/swift-java that referenced this issue Nov 9, 2024
This is not a real fix, I suspect the real fix is to remove javaEnvironment as
an instance variable on all platforms and always use the ambient environment
(as the JNI specification clearly states the environment cannot be shared
between threads).

Works around: swiftlang#157
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants