vapor-community / sockets

🔌 Non-blocking TCP socket layer, with event-driven server and client.
MIT License
575 stars 54 forks source link

Crash when setsockopt fails #83

Closed andreasley closed 7 years ago

andreasley commented 7 years ago

I've seen the following crash in a Vapor project I'm working on, running on OS X 10.11.6: fatal error: 'try!' expression unexpectedly raised an error: Socket failed with code 22 ("Invalid argument") [optionSetFailed(65535, 4, "1")]

Here's part of the relevant stack trace:

0   libswiftCore.dylib              0x000000010a46a3c0 _TFs16_assertionFailedFTVs12StaticStringSSS_Su5flagsVs6UInt32_Os5Never + 144
1   libswiftCore.dylib              0x000000010a48e249 swift_unexpectedError_merged + 585
2   SocksCore                       0x000000010a33da7a _TFE9SocksCorePS_9RawSockets12reuseAddressSb + 234
3   SocksCore                       0x000000010a33feda _TFC9SocksCore17TCPInternetSocketcfzT10descriptorGSqVs5Int32_6configVS_12SocketConfig7addressCS_23ResolvedInternetAddress_S0_ + 394
4   SocksCore                       0x000000010a33fcbf _TFC9SocksCore17TCPInternetSocketCfzT10descriptorGSqVs5Int32_6configVS_12SocketConfig7addressCS_23ResolvedInternetAddress_S0_ + 159
5   SocksCore                       0x000000010a341300 _TFC9SocksCore17TCPInternetSocket6acceptfzT_S0_ + 448
6   Transport                       0x00000001092306c5 _TFC9Transport15TCPServerStream6acceptfzT_PS_6Stream_ + 149
7   Transport                       0x0000000109230ad3 _TTWC9Transport15TCPServerStreamS_12ServerStreamS_FS1_6acceptfzT_PS_6Stream_ + 51
8   HTTP                            0x0000000109f135a2 _TFC4HTTP6Server5startfzT9responderPS_9Responder_6errorsFOS_11ServerErrorT__T_ + 322
9   HTTP                            0x0000000109f149b3 _TTWu1_Rx9Transport12ServerStream_S_14TransferParser0_S_18TransferSerializerw_11MessageTypezC4HTTP7Requestw0_11MessageTypezCS4_8ResponserGCS4_6Serverxq_q0__S4_14ServerProtocolS4_FS9_5startfzT9responderPS4_9Responder_6errorsFOS4_11ServerErrorT__T_ + 67
10  HTTP                            0x0000000109efa0b0 _TZFE4HTTPPS_14ServerProtocol5startfzT4hostGSqSS_4portGSqSi_13securityLayerO9Transport13SecurityLayer9responderPS_9Responder_6errorsFOS_11ServerErrorT__T_ + 800
11  Vapor                           0x0000000109ff0cfb _TFC5Vapor7Droplet10bootServerfzT4nameSS12isLastServerSb_T_ + 7099
12  Vapor                           0x0000000109fec949 _TFC5Vapor7Droplet11bootServersfzT_T_ + 2425

The corresponding call is here in SocketOptions.swift:

try! Self.setBoolOption(descriptor: descriptor,
                   level: SOL_SOCKET,
                    name: SO_REUSEADDR,
                    value: newValue)

For some reason, setsockopt fails under certain circumstances, setOption throws and since the result of setBoolOption is forcibly unwrapped, I end up with a EXC_BAD_INSTRUCTION .

There's the following comment in the same file: When we have throwing property setters, remove the bangs below Since throwing setters will not be available in the near future, would you be interested in a pull request where the properties are replaced by functions?

I will investigate the reason why setsockopt sometimes fails in my environment, but this might take a while.

tanner0101 commented 7 years ago

I think moving these to functions would be the right thing to do, but that would break public API forcing us to bump the major.

Is there any way to fix this issue for 1.0? Perhaps catching the error and printing a warning for now.

And for Socks 2.0 we can move to functions.

andreasley commented 7 years ago

@tannernelson I've created a PR: https://github.com/vapor/socks/pull/85

andreasley commented 7 years ago

Resolved in release 1.1.0