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

HttpComponentsClientHttpConnector hangs when host is invalid #29156

Closed
dreis2211 opened this issue Sep 14, 2022 · 0 comments
Closed

HttpComponentsClientHttpConnector hangs when host is invalid #29156

dreis2211 opened this issue Sep 14, 2022 · 0 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@dreis2211
Copy link
Contributor

dreis2211 commented Sep 14, 2022

Hi,

today we spent quite a bit of time debugging a hang in the WebClient in combination with using Apache's HttpComponentsClientHttpConnector that essentially boils down to the following:
(I've put this also into an example under https://github.com/dreis2211/spring-httpclient-hang-demo for simpler reproduction)

	@Test
	@Timeout(value = 5)
	void testWebClient() {
		WebClient client = WebClient.builder().apply(customize()).build();
		client.get()
				.uri("/path")
				.retrieve()
				.bodyToMono(Void.class)
				.block();
	}

	private Consumer<WebClient.Builder> customize() {
		return builder -> {
			ClientHttpConnector connector = buildClientConnector();
			builder.clientConnector(connector);
			builder.baseUrl(null); // Leads to invalid host
		};
	}

	private ClientHttpConnector buildClientConnector() {
		HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();
		RequestConfig config = RequestConfig.custom()
				.setConnectTimeout(org.apache.hc.core5.util.Timeout.ofMilliseconds(1000))
				.setResponseTimeout(org.apache.hc.core5.util.Timeout.ofMilliseconds(2000))
				.build();
		clientBuilder.setDefaultRequestConfig(config);
		return new HttpComponentsClientHttpConnector(clientBuilder.build());
	}

It seems that the empty or nulled baseUrl should lead to an exception like the following, but the exception seems to be swallowed and the client request hangs afterwards.

Caused by: org.apache.hc.core5.http.ProtocolException: Target host is not specified
	at org.apache.hc.client5.http.impl.routing.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.java:65)
	at org.apache.hc.client5.http.impl.async.InternalHttpAsyncClient.determineRoute(InternalHttpAsyncClient.java:113)
	at org.apache.hc.client5.http.impl.async.InternalAbstractHttpAsyncClient$1.sendRequest(InternalAbstractHttpAsyncClient.java:195)

It might be very well a problem of Apache's HttpClient, but a quick look at https://github.com/apache/httpcomponents-client/blob/5.1.x/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttpExchange.java#L67 and commenting the line leads to the expected exception and no hang.

Unfortunately, my investigation time is limited and I hope you can take over from here.

The issue is obviously a misconfiguration of the WebClient in our particular case (and already fixed), but the hang is somewhat unexpected and dangerous. Especially if timeouts are configured one expects no infinite blocking.

Let me know if you need anything.

Cheers,
Christoph

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 14, 2022
@dreis2211 dreis2211 changed the title WebClient hangs with HttpComponentsClientHttpConnector when host is not valid WebClient hangs indefinitely with HttpComponentsClientHttpConnector when host is not valid Sep 19, 2022
@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Jan 24, 2023
@poutsma poutsma self-assigned this Jan 31, 2023
@poutsma poutsma added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 31, 2023
@poutsma poutsma added this to the 6.0.5 milestone Jan 31, 2023
@poutsma poutsma changed the title WebClient hangs indefinitely with HttpComponentsClientHttpConnector when host is not valid HttpComponentsClientHttpConnector hangs host is invalid Jan 31, 2023
@poutsma poutsma changed the title HttpComponentsClientHttpConnector hangs host is invalid HttpComponentsClientHttpConnector hangs when host is invalid Jan 31, 2023
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
This commit ensures that we register a result callback when executing
the request (next to the existing response callback), which gets
notified of invalid hosts and other connection issues.

Closes spring-projectsgh-29156
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants