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

HTTPSClient error "Connection reset by peer" when making request #81

Closed
donut opened this issue Apr 21, 2016 · 14 comments
Closed

HTTPSClient error "Connection reset by peer" when making request #81

donut opened this issue Apr 21, 2016 · 14 comments

Comments

@donut
Copy link

donut commented Apr 21, 2016

Gist
Download project

With snapshot 2016-4-12-a on OS X, I get "Connection reset by peer" when running this code:

import HTTPSClient

let client = try HTTPSClient.Client(uri: "https://www.googleapis.com:443")
let response = try client.get(
    "/youtube/v3/videos?part=snippet,statistics&id=1,2&key=REDACTED")

The full error:

[I] ~/work/HTTPSClient_issue                                                    
↪ ./.build/debug/HTTPClient_issue                                     0@10:36:38
fatal error: Error raised at top level: Connection reset by peer: file /Users/buildnode/jenkins/workspace/oss-swift-package-osx/swift/stdlib/public/core/ErrorType.swift, line 59
Current stack trace:
0   libswiftCore.dylib                  0x00000001007376a1 swift_reportError + 145
1   libswiftCore.dylib                  0x0000000100754a17 _swift_stdlib_reportFatalErrorInFile + 103
2   libswiftCore.dylib                  0x000000010070a193 partial apply forwarder for Swift.(_assertionFailed (Swift.StaticString, Swift.String, Swift.StaticString, Swift.UInt, flags : Swift.UInt32) -> ()).(closure #1).(closure #1).(closure #1) + 99
3   libswiftCore.dylib                  0x000000010070a1ef partial apply forwarder for reabstraction thunk helper from @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> () to @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> (@out ()).291 + 63
4   libswiftCore.dylib                  0x00000001005c09bd generic specialization <preserving fragile attribute, ()> of Swift.StaticString.withUTF8Buffer <A> (invoke : (Swift.UnsafeBufferPointer<Swift.UInt8>) -> A) -> A + 349
5   libswiftCore.dylib                  0x00000001006cf77c function signature specialization <preserving fragile attribute, Arg[1] = Owned To Guaranteed and Exploded> of Swift.(_assertionFailed (Swift.StaticString, Swift.String, Swift.StaticString, Swift.UInt, flags : Swift.UInt32) -> ()).(closure #1) + 204
6   libswiftCore.dylib                  0x00000001006e280f partial apply forwarder for Swift.(_assertionFailed (Swift.StaticString, Swift.String, Swift.StaticString, Swift.UInt, flags : Swift.UInt32) -> ()).(closure #1) + 143
7   libswiftCore.dylib                  0x00000001006adb9a function signature specialization <preserving fragile attribute, Arg[1] = Exploded> of Swift._assertionFailed (Swift.StaticString, Swift.String, Swift.StaticString, Swift.UInt, flags : Swift.UInt32) -> () + 218
8   libswiftCore.dylib                  0x00000001005d6cff swift_errorInMain + 719
9   HTTPClient_issue                    0x00000001000bd243 main + 2403
10  libdyld.dylib                       0x00007fff962145ad start + 1
11  ???                                 0x0000000000000001 0x0 + 1
fish: './.build/debug/HTTPClient_issue' terminated by signal SIGILL (Illegal instruction)
@donut
Copy link
Author

donut commented Apr 22, 2016

Still getting this with HTTPSClient 0.5.2. Originally found the issue with 0.5.0.

@donut
Copy link
Author

donut commented Apr 27, 2016

Tried this with @tomohisa's fork which apparently fixes some issues but it failed in the same way.

@tomohisa
Copy link
Member

@donut looks like its different issue, I see many issues happened in openssl. I'm gonna try to debug it little bit

@tomohisa
Copy link
Member

https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&id=1,2&key=REDACTED
url replies in my safari.
{ "error": { "errors": [ { "domain": "usageLimits", "reason": "keyInvalid", "message": "Bad Request" } ], "code": 400, "message": "Bad Request" } }
Is this what you want to get?
looks like c library libmill reply this I still not found why it return Connection reset by peer error

@donut
Copy link
Author

donut commented Apr 27, 2016

That is the expected result when key is invalid. Also, the id values are likely invalid.

@tomohisa
Copy link
Member

I see. I don't have keys so I can't test fully but yeah, still crash before I get that result

@tomohisa
Copy link
Member

tomohisa commented Apr 27, 2016

Hey I have progress. I put print everywhere it goes and after

decriptedData += try ssl.read()
print("\(#file)(\(#line)):\(#function) decriptedData:\(decriptedData)")

it print following.

Since its already return bad request, it should close connection, but eventually it doesn't. And trying to receive more and it returns connection reset by peer. so we need to write down code to close and return request.

0.5.0/Sources/OpenSSL/ClientStream.swift(94):receive(upTo:timingOut:) decriptedData:HTTP/1.1 400 Bad Request
Vary: X-Origin
Content-Type: application/json; charset=UTF-8
Date: Wed, 27 Apr 2016 16:57:32 GMT
Expires: Wed, 27 Apr 2016 16:57:32 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alternate-Protocol: 443:quic
Alt-Svc: quic=":443"; ma=2592000; v="32,31,30,29,28,27,26,25"
Accept-Ranges: none
Vary: Origin,Accept-Encoding
Connection: close

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "keyInvalid",
    "message": "Bad Request"
   }
  ],
  "code": 400,
  "message": "Bad Request"
 }
}```

tomohisa pushed a commit to tomohisa/HTTPSClient that referenced this issue Apr 27, 2016
tomohisa pushed a commit to tomohisa/TCPSSL that referenced this issue Apr 27, 2016
tomohisa pushed a commit to tomohisa/TCP that referenced this issue Apr 27, 2016
@tomohisa
Copy link
Member

This is workaround project. It just works with my forks (but only really changed TCP)
(HTTPSClient and TCPSSL changed only for reaching dependency to TCP)
https://www.dropbox.com/s/xeom1s4a0vgffnv/HTTPSClient_issue_fixed.zip?dl=0
@donut this is temporary workaround.
make build run on folder
Maybe you can put your key and id and test it.

Reason

TCP return reset after finishing SSL request
http://stackoverflow.com/questions/2974021/what-does-econnreset-mean-in-the-context-of-an-af-local-socket

Workaround

          do {
            try ensureLastOperationSucceeded()
          } catch SystemError.connectionResetByPeer {
            throw StreamError.closedStream(data:Data())
          }

https://github.com/tomohisa/TCP/blob/46a45992c60f7a8c5b264a9dcabe7ddbef918934/Source/TCPConnection.swift#L92

@paulofaria I just assumed if server return connection reset, it means closed connection. It helps to read whole data without exception above. Do you think this workaround is good as framework? or we need more condition to determine if it is really closed (which I think it might be difficult)

@paulofaria
Copy link
Member

@tomohisa yeah. that's exactly the fix I was going to do. except that the data inside closedStream should be Data(data.prefix(received)).

@tomohisa
Copy link
Member

Cool let me try with what you said

@donut
Copy link
Author

donut commented Apr 27, 2016

That worked both with and without a valid key.

@donut
Copy link
Author

donut commented Apr 28, 2016

@paulofaria Is there a reason you're holding off tagging VeniceX/TCP after the merge?

@paulofaria
Copy link
Member

@donut Yeah there is. I forgot to tag it. hahaha.

@donut
Copy link
Author

donut commented Apr 28, 2016

After the merge, everything is working as expected. Marking this as closed.

@donut donut closed this as completed Apr 28, 2016
tomohisa added a commit to ZewoGraveyard/TCP that referenced this issue May 6, 2016
* testing for https crash

workaround for

Zewo/Zewo#81 (comment)

* HTTPS closed on some client

some client reset tcp connection after finish sending ssl response.

To avoid exception, when reset connection, close stream and it go
through fine.

* update for 05-03 snapshot

only changes dependency

* change .swift-version

* Update for 2016-05-03
tomohisa added a commit to ZewoGraveyard/TCP that referenced this issue May 9, 2016
* testing for https crash

workaround for

Zewo/Zewo#81 (comment)

* HTTPS closed on some client

some client reset tcp connection after finish sending ssl response.

To avoid exception, when reset connection, close stream and it go
through fine.

* update for 05-03 snapshot

only changes dependency

* change .swift-version

* Update for 2016-05-03
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

No branches or pull requests

3 participants