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

Attempt 3 at cleaning up the threadlocal leaks #206

Merged
merged 2 commits into from
Feb 8, 2024
Merged

Conversation

jbellis
Copy link
Owner

@jbellis jbellis commented Feb 7, 2024

No description provided.

@jkni
Copy link
Collaborator

jkni commented Feb 8, 2024

I like the improved explanations, simplified code from abandoning the try-with-resources/PoolingSupport, and robustness against circular references in the value of ETL.

Going forward, I think we'll want to be careful about our use of ForkJoinPools, particularly the common pool. If an ETL is used on these pools, the thread lifetime is uncontrolled. This is particularly true for the commonPool, which may grow or shrink significantly based on other usage in the application.

* The standard {@link ThreadLocal} appears to be designed to be used with relatively
* short-lived Threads. Specifically, it uses a ThreadLocalMap to store ThreadLocal key/value
* Entry objects, and there are no guarantees as to when Entry references are expunged unless
* you can explicitly call remove() on the ThreadLocal instance. This means that objects
Copy link
Collaborator

@jkni jkni Feb 8, 2024

Choose a reason for hiding this comment

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

This means that objects referenced by ThreadLocals will not be able to GC'd for the lifetime of the Thread.

I don't think this is quite true. The inability to be GCed for the lifetime of the thread is only true if the object containing the ThreadLocal is circularly referenced by the value.

Copy link
Owner Author

Choose a reason for hiding this comment

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

My reasoning: TLM has a strong reference to its Entry[] and Entry has a strong reference to value. Therefore you can't GC value while the Entry exists.

        static class Entry extends WeakReference<ThreadLocal<?>> {
            /** The value associated with this ThreadLocal. */
            Object value;

            Entry(ThreadLocal<?> k, Object v) {
                super(k);
                value = v;
            }
        }

@jkni
Copy link
Collaborator

jkni commented Feb 8, 2024

LGTM after license header is added to ETL.

@jbellis jbellis force-pushed the ExplicitThreadLocal2 branch from 3e55d29 to 217912f Compare February 8, 2024 18:06
@jbellis jbellis merged commit 240770c into main Feb 8, 2024
4 checks passed
@jbellis jbellis deleted the ExplicitThreadLocal2 branch February 8, 2024 18:06
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.

2 participants