Skip to content

Prevent conn leak on thread interrupt#6835

Open
dagnir wants to merge 1 commit intomasterfrom
dongie/apache5-connection-leak-interrupt
Open

Prevent conn leak on thread interrupt#6835
dagnir wants to merge 1 commit intomasterfrom
dongie/apache5-connection-leak-interrupt

Conversation

@dagnir
Copy link
Copy Markdown
Contributor

@dagnir dagnir commented Mar 31, 2026

Motivation and Context

If a thread blocked waiting on a connection pool is interrupted, it's possible for the connection to become leaked if the pool then completes the future from its side.

This commit prevents that from happening by intercepting the interrupt and attempting to cancel the lease future. If that is unsuccessful, the connection is returned to the upper, and the thread is reinterrupted.

Adresses #6786

Modifications

Testing

  • New wiremock test

Screenshots (if appropriate)

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

If a thread blocked waiting on a connection pool is interrupted, it's
possible for the connection to become leaked if the pool then completes
the future from its side.

This commit prevents that from happening by intercepting the interrupt
and attempting to cancel the lease future. If that is unsuccessful, the
connection is returned to the upper, and the thread is reinterrupted.

Adresses #6786
@dagnir dagnir requested a review from a team as a code owner March 31, 2026 21:41
@dagnir dagnir added the no-api-surface-area-change Indicate there is no API surface area change and thus API surface area review is not required label Mar 31, 2026
@dagnir
Copy link
Copy Markdown
Contributor Author

dagnir commented Mar 31, 2026

dagnir added the no-api-surface-area-change label

There's a change in Apache5HttpClient but only to internal methods.

@Test
void leaseInterrupted_doesNotLeakConnections_probabilistic() throws IOException, InterruptedException, ParseException {
wm.stubFor(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withStatus(204)));
int rounds = 16;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

With the PoolingHttpClientConnectionManagerBuilder, this fails within the first 3 iterations typically

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-api-surface-area-change Indicate there is no API surface area change and thus API surface area review is not required

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant