apache / apache/lucene

The Closeable interface of CloseableThreadLocal<T> seems redundent

Open
#13,451 0 comments 0 reactions 0 assignees View on GitHub
type:enhancement
Dominant language
Java
Stars
3.6k
Forks
1.4k
Avg merge
2d 11h
Merged PRs (30d)
88

Description

### Description

## Description
While reading the source code, I noticed that the `CloseableThreadLocal` implementation should release the stored hard references even without invoking `close()`. To illustrate this, I created the following code example.
```
CloseableThreadLocal ctl = new CloseableThreadLocal<>();
Object value = new Object();
ctl.set(value);
WeakReference valueRef = new WeakReference<>(value);
value = null;
System.gc();
assert valueRef.get() != null;
ctl = null;
System.gc();
assert valueRef.get() == null;
```
This approach is advantageous because it removes the necessity to maintain references and manually invoking close() for classes that using CloseableThreadLocal e.g. Analyzer.

Contributor guide

Open the contributing guide

Research direction

Start by reading the CloseableThreadLocal implementation and the Analyzer usage mentioned in the issue, then verify the WeakReference example and how values are retained after the owning reference is dropped. Determine whether the Closeable interface and explicit close lifecycle can be removed without changing required cleanup behavior, and add or update coverage showing the intended reference release.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
search
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.