Skip to content

Conversation

@madsodgaard
Copy link
Contributor

@madsodgaard madsodgaard commented Nov 28, 2025

Currently, interacting with any AnyJavaObject is only possible from the thread that created it. This is quite limiting, as its often needed to pass these objects across threads. This limitation also affects features like the protocol bridging, as we use wrap-Java to callback to Java from Swift, and they will cross threads.

The reason this is, is that currently all the method call helpers use the javaEnvironment from JavaObjectHolder, which is the environment that was used when the object was created. I don't see a reason why we should use this environment to interact with the object, after its creation. We should really be using the current threads environment instead, otherwise the JVM will crash.

This PR also fixes #157

Copy link
Collaborator

@ktoso ktoso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable, got any test that failed before and will now work to add here tho?

@madsodgaard
Copy link
Contributor Author

@ktoso I added a simple test

@ktoso
Copy link
Collaborator

ktoso commented Nov 29, 2025

Neat, lgtm!

@ktoso ktoso enabled auto-merge (squash) November 29, 2025 07:17
@ktoso ktoso merged commit c0c6fd5 into swiftlang:main Dec 1, 2025
94 of 96 checks passed
@madsodgaard madsodgaard deleted the jni-threads branch December 1, 2025 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JavaKit: Passing objects between threads

2 participants