Skip to content

Do not permanently attach JVM threads#185

Merged
complexspaces merged 2 commits into
mainfrom
android-threading
Aug 13, 2025
Merged

Do not permanently attach JVM threads#185
complexspaces merged 2 commits into
mainfrom
android-threading

Conversation

@complexspaces

Copy link
Copy Markdown
Collaborator

We are a library and could be used in any kind of threading context, so we should leave the thread attachment behavior (a performance optimization) choice up to the caller. I did, however, also reduce the number of attachments we call down to 1 as a performance optimization since the existing patterns we have allow it to be safe.

Closes #184

@andrew-signal

andrew-signal commented Aug 6, 2025

Copy link
Copy Markdown

(Following up, as you asked us to try this out in the other issue.)

Currently, I'm not seeing any JVM heap size growth in my testbed app that exhibited the issue earlier after thirty minutes of running. (The test bed app is a simple app that opens a connection that uses this library every few seconds or so, and then closes it down, and tries again in a loop. Before this, we would see unbounded heap growth until an eventual OOM crash as the verifier ran on a worker thread from a thread pool, and the other tasks on the thread could depend on the JVM de-attach to clean up their memory usage, as Jordan discussed in #184.)

I plan to run the app over night in a "soak test", but this seems to be an early indication that this does in fact fix the issue, as expected.

Thanks again for the quick fix!

@djc djc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Weak approval: I think this makes sense but I don't have much expertise in JVM matters to review this thoroughly.

@complexspaces complexspaces added bug Something isn't working O-Android Work related to the Android verifier implementation labels Aug 13, 2025

@banasiak banasiak left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hello friends. @complexspaces asked me to take a look at this PR. I agree that this is a good idea for the library as it will allow the thread to be released when it goes out of scope. 👍

@complexspaces

complexspaces commented Aug 13, 2025

Copy link
Copy Markdown
Collaborator Author

I took a look at the CI test failures:

  • Windows: This seems to be Test failures on main - suspected Windows verifier weirdness #117 and I can reproduce them on main so it wasn't caused by changes in this branch.
  • Android: The only regressions I would have expected in this branch would have stopped any kind of verification (UAFs, failure to lookup classes at runtime, etc). The only ones here are the usual expiry failures.

Given that analysis, I believe this is fine to merge and then followup on the certificate rotation when time permits.

We are a library and may be used in any kind of threading context, so leave this performance choice up to the caller.

Additionally, optimize env access patterns to be more clear how many times
we try attaching.
@complexspaces

Copy link
Copy Markdown
Collaborator Author

I've rebased this on main to pick up the CI fixes and stable Clippy lint changes. Nothing else was changed during the rebase.

@complexspaces
complexspaces merged commit 80b25f7 into main Aug 13, 2025
21 checks passed
@complexspaces
complexspaces deleted the android-threading branch August 13, 2025 21:34
@complexspaces complexspaces mentioned this pull request Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working O-Android Work related to the Android verifier implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Android] Don't use attach_current_thread_permanently, leave that decision up to the client

4 participants