Skip to content

HttpsUrlConnections hang (then respond correctly) when setUseEngineSocket is true #433

Closed

Description

@carterkozak

Repro pushed here using a simple webserver and HttpsUrlConnections to execute requests.
https://github.com/cakofony/conscrypt-httpurlconnection-repro

Using conscrypt 1.0.1.

When setUseEngineSocket is false, things work great.
When setUseEngineSocket is true, all requests using HttpsUrlConnection wait exactly 60 seconds, then respond with the expected 200 response.

https://github.com/cakofony/conscrypt-httpurlconnection-repro/blob/master/src/test/java/net/ckozak/TestConscrypt.java#L88-L94

Activity

  1. carterkozak commented on Feb 28, 2018

    @carterkozak
    Author

    Tagging folks who have been involved in this conversation: @nmittler @flooey @yschimke

  2. carterkozak commented on Mar 12, 2018

    @carterkozak
    Author

    @nmittler @flooey Were you able to reproduce this issue? I'd be happy to provide assistance if you're running into issues.

  3. nmittler commented on Mar 12, 2018

    @nmittler
    Contributor

    @flooey mind taking a look?

  4. flooey commented on Mar 29, 2018

    @flooey
    Contributor

    I finally got a chance to diagnose this and it looks like it's due to HTTP keep-alive combined with the lack of nonblocking I/O on InputStream. The peer doesn't close the socket for 60 seconds because of HTTP keep-alive. If we happen to give a buffer to InputStream.read() that's larger than the entire encrypted stream, it will block until the buffer is full or the socket is closed, which means it waits until the keep-alive times out.

    There are a couple ways we could try to work around this.

    We could try to read only a single byte from the socket (which will block), and when that returns, check how many bytes are available and just read those. That will probably eliminate a bunch of the situations where this blocks needlessly, but relies on available() working, which isn't guaranteed.

    We also could use non-blocking Channels, which requires us to not rely on Socket's underlying connection machinery and instead create our own SocketChannel and connect that. It won't work when we're asked to wrap an existing Socket.

    I'm going to start with the first one and see if that'll work.

  5. flooey commented on Mar 29, 2018

    @flooey
    Contributor

    Actually, at this point, I'm not sure why the FD socket isn't taking 60 seconds to return. The same combination of factors applies there; just because we're reading the file descriptor directly shouldn't change anything, we're still being asked to just keep reading the file descriptor. Looking at a packet trace, though, the server sends a FIN immediately after finishing sending the HTTP response when it's dealing with the FD socket, but it waits for 60 seconds to do so when dealing with the engine socket, and which one we're using should be totally opaque to the server. Very curious.

  6. dave-r12 commented on Sep 10, 2018

    @dave-r12

    @flooey if I add a Thread.sleep(..) to the testDefault() method then the FIN is not sent immediately. I'm thinking it's a consequence of the test ending and the VM being destroyed. What do you think?

  7. dave-r12 commented on Sep 10, 2018

    @dave-r12

    I'd love to see #65 get resolved. If there is anything I can do to help with that let me know.

    I created a third test:

        @Test
        public void nonConscrypt() {
            try {
                HttpsURLConnection connection = (HttpsURLConnection) new URL("https://localhost:" + PORT + "/ping").openConnection();
                connection.setSSLSocketFactory(sunJsseContext.getSocketFactory());
                connection.setHostnameVerifier((s, sslSession) -> true);
                try (InputStream response = connection.getInputStream()) {
                    new String(ByteStreams.toByteArray(response), StandardCharsets.UTF_8);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    It seems the Java implementation is using SocketInputStream as well. The difference that I see is with the Java implementation it appears to be reading SSL/TLS records (with well defined length) vs. Conscrypt which is just reading in a buffer. Is it possible to change Conscrypt to read record by record?

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions