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

Update AttributeMap.java #4404

Closed
wants to merge 1 commit into from
Closed

Conversation

RANJITHROSAN17
Copy link

Motivation and Context

This change is motivated by the need to prevent a deadlock issue that occurs in JDK 19+ when attempting to close an ExecutorService included in the AttributeMap's close method. The current implementation can lead to a deadlock when closing resources. To resolve this issue, the code has been modified to explicitly exclude ExecutorService instances from the AutoCloseable loop in AttributeMap::close.

Modifications

  • Modified the AttributeMap::close method to separate ExecutorService instances from other AutoCloseable resources, ensuring that they are not closed together. This change prevents potential deadlocks when closing resources.
  • Introduced the shutdownIfExecutorService method to explicitly shut down ExecutorService instances if needed.

Testing

The changes have been tested thoroughly in a development environment. Testing included:

  • Verifying that the deadlock issue no longer occurs when closing the client and ExecutorService in the same thread.
  • Ensuring that all existing tests continue to pass without issues.
  • Validating that the modified code does not introduce new problems or regressions.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • I have added a changelog entry. Adding a new entry must be accomplished by running the scripts/new-change script and following the instructions. Commit the new file created by the script in .changes/next-release with your changes.
  • My change is to implement 1.11 parity feature, and I have updated LaunchChangelog

License

  • I confirm that this pull request can be released under the Apache 2 license

@RANJITHROSAN17 RANJITHROSAN17 requested a review from a team as a code owner September 7, 2023 17:29
Copy link
Contributor

@scrocquesel scrocquesel left a comment

Choose a reason for hiding this comment

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

Thanks, I was doing a similar PR to resolve #4395

attributes.values().forEach(this::shutdownIfExecutorService);
}
public void close() {
attributes.values().stream()
Copy link
Contributor

Choose a reason for hiding this comment

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

I would comment why we need to explicitely exclude ExecutorService

Suggested change
attributes.values().stream()
// Starting from JDK 19, ExecutorService implements AutoCloseable and will await termination on close
// When ExecutorService is provided by the user, it can result in thread deadlocks if the ExecutorService
// is not closed from another thread. ExecutorService are shutdown without waiting for termination
attributes.values().stream()

@debora-ito debora-ito added the needs-review This issue or PR needs review from the team. label Sep 13, 2023
.filter(value -> !(value instanceof ExecutorService))
.forEach(v -> IoUtils.closeIfCloseable(v, null));
attributes.values().stream()
.filter(value -> value instanceof ExecutorService)
Copy link

@tiny-george tiny-george Sep 29, 2023

Choose a reason for hiding this comment

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

Well, do a filter and later check again in shutdownIfExecutorService method is not a good deal. I would remove this filter or change the method.

I prefer something like this, instead of iterate twice:

attributes.values().forEach(attribute -> attribute instanceof ExecutorService ?
 ((ExecutorService)attribute.shutdown()) : IoUtils.closeIfCloseable(attribute, null));

@dagnir dagnir self-assigned this Oct 26, 2023
@dagnir
Copy link
Contributor

dagnir commented Oct 26, 2023

Hi, thanks for the PR. This change makes sense to me; I like @tiny-george's suggestion, can you please implement that?

@michaeldimchuk
Copy link
Contributor

Hi, thanks for the PR. This change makes sense to me; I like @tiny-george's suggestion, can you please implement that?

@dagnir Doesn't look like this PR has been updated in a while, so I created another one that addresses the requested changes and adds some tests: #4649. Mind taking a look?

@dagnir
Copy link
Contributor

dagnir commented Nov 10, 2023

Closing this as #4649 has been merged. Thank you for the PR @RANJITHROSAN17.

@dagnir dagnir closed this Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-review This issue or PR needs review from the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants