{"action":"opened","issue":{"url":"https://api.github.com/repos/httpswift/swifter/issues/201","repository_url":"https://api.github.com/repos/httpswift/swifter","labels_url":"https://api.github.com/repos/httpswift/swifter/issues/201/labels{/name}","comments_url":"https://api.github.com/repos/httpswift/swifter/issues/201/comments","events_url":"https://api.github.com/repos/httpswift/swifter/issues/201/events","html_url":"https://github.com/httpswift/swifter/issues/201","id":180891208,"number":201,"title":"`Darwin.shutdown` doesn't actually close the socket","user":{"login":"allen-zeng","id":7380931,"avatar_url":"https://avatars.githubusercontent.com/u/7380931?v=3","gravatar_id":"","url":"https://api.github.com/users/allen-zeng","html_url":"https://github.com/allen-zeng","followers_url":"https://api.github.com/users/allen-zeng/followers","following_url":"https://api.github.com/users/allen-zeng/following{/other_user}","gists_url":"https://api.github.com/users/allen-zeng/gists{/gist_id}","starred_url":"https://api.github.com/users/allen-zeng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/allen-zeng/subscriptions","organizations_url":"https://api.github.com/users/allen-zeng/orgs","repos_url":"https://api.github.com/users/allen-zeng/repos","events_url":"https://api.github.com/users/allen-zeng/events{/privacy}","received_events_url":"https://api.github.com/users/allen-zeng/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-10-04T13:21:42Z","updated_at":"2016-10-04T13:21:42Z","closed_at":null,"body":"I'm writing a few iOS unit tests that rely on Swifter. The structure looks like the following:\r\n```swift\r\nclass HttpClientTests: XCTestCase {\r\n    private var server: HttpServer!\r\n\r\n    override func setUp() {\r\n        server = HttpServer()\r\n    }\r\n\r\n    override func tearDown() {\r\n        server.stop()\r\n    }\r\n\r\n    func testA() {\r\n        do {\r\n             try server.start()\r\n        } catch SocketError.bindFailed(let message) {\r\n            XCTFail(message)\r\n        } catch {\r\n            XCTFail(error)\r\n        }\r\n\r\n        // rest of the test\r\n    }\r\n\r\n    // other tests that also begin with starting the server\r\n}\r\n```\r\n\r\nThe first test will always successfully start the Swifter server, however subsequent tests always fail with the error `SocketError.listenFailed(\"Address already in use\")`.\r\n\r\nI'm running Xcode 8 in macOS Sierra, running the Swift 3 compatible version at commit [d18f01](https://github.com/httpswift/swifter/tree/d18f0187970915c753d1dfc5cd94da5f8254c189) (latest at the time of writing).\r\n\r\nThe error is obviously thrown [here](https://github.com/httpswift/swifter/blob/d18f0187970915c753d1dfc5cd94da5f8254c189/Sources/Socket+Server.swift#L77). But what's more interesting is how it's shut down in the function [`release`](https://github.com/httpswift/swifter/blob/d18f0187970915c753d1dfc5cd94da5f8254c189/Sources/Socket.swift#L180): it consistently produces `-1`, which causes the following statement `close(socket)` to not be called.\r\n\r\nRemoving the `if` statement makes the tests pass, i.e. the server gets started and stopped correctly.\r\n\r\nHaving had a look at the documentation on [`shutdown`](http://www.manpages.info/macosx/shutdown.2.html), I looked into the actual error by printing `errno`, it consistently produces `57`, which according to [this page](https://www.freebsd.org/cgi/man.cgi?query=errno&sektion=2) maps to `ENOTCONN`. This value also exists in the macOS [`shutdown` man page](http://www.manpages.info/macosx/shutdown.2.html):\r\n> The call succeeds unless:\r\n...\r\n[ENOTCONN]\r\nThe specified socket is not connected. \r\n\r\nI'm not entirely sure what the solution might be. Could anyone take a look?\r\n\r\nThanks in advance."}}
{"actor":{"display_login":"allen-zeng"}}