Skip to content

Ensure Thread can be collected in a timely manner if Recycler.Stack h…#7499

Closed
normanmaurer wants to merge 1 commit into4.1from
reycler_stack_thread_ref
Closed

Ensure Thread can be collected in a timely manner if Recycler.Stack h…#7499
normanmaurer wants to merge 1 commit into4.1from
reycler_stack_thread_ref

Conversation

@normanmaurer
Copy link
Copy Markdown
Member

…olds a reference to it.

Motivation:

In our Recycler implementation we store a reference to the current Thread in the Stack that is stored in a FastThreadLocal. The Stack itself is referenced in the DefaultHandle itself. A problem can arise if a user stores a Reference to an Object that holds a reference to the DefaultHandle somewhere and either not remove the reference at all or remove it very late. In this case the Thread itself can not be collected as its still referenced in the Stack that is referenced by the DefaultHandle.

Modifications:

  • Use a WeakReference to store the reference to the Thread in the Stack
  • Add a test case

Result:

Ensure a Thread can be collected in a timely manner in all cases even if it used the Recycler.

…olds a reference to it.

Motivation:

In our Recycler implementation we store a reference to the current Thread in the Stack that is stored in a FastThreadLocal. The Stack itself is referenced in the DefaultHandle itself. A problem can arise if a user stores a Reference to an Object that holds a reference to the DefaultHandle somewhere and either not remove the reference at all or remove it very late. In this case the Thread itself can not be collected as its still referenced in the Stack that is referenced by the DefaultHandle.

Modifications:

- Use a WeakReference to store the reference to the Thread in the Stack
- Add a test case

Result:

Ensure a Thread can be collected in a timely manner in all cases even if it used the Recycler.
@normanmaurer
Copy link
Copy Markdown
Member Author

@ejona86 whatever we do with #7463 we should merge this one in all cases :)

Copy link
Copy Markdown
Member

@carl-mastrangelo carl-mastrangelo left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Member

@Scottmitch Scottmitch left a comment

Choose a reason for hiding this comment

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

lgtm

thread = null;

// Loop until the Thread was collected. If we can not collect it the Test will fail due of a timeout.
while (!collected.get()) {
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.

This test is a bit speculative, and dependent upon VM/GC behavior. Hopefully all GCs behaves as expected and will reclaim/finalize the object in time. I'm not sure of a better way to test this, but this maybe subject to sporadic test failures (maybe unlikely though?).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@Scottmitch I think its the best we can do... If there will be ever a better API I am happy to revisit it.

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.

If it starts failing I guess we can take a closer look and consider disabling it if necessary.

@normanmaurer
Copy link
Copy Markdown
Member Author

Cherry-picked into 4.1 (0276b6e) and 4.0 (b386ee3)

@normanmaurer normanmaurer deleted the reycler_stack_thread_ref branch December 14, 2017 05:50
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.

5 participants