-
Notifications
You must be signed in to change notification settings - Fork 198
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
Socket not cleaned up on timeout; some socket descriptors cause crashes #120
Comments
You’ve given me a GREAT explanation. I wish they were all so good. I’ve got some minor surgery tomorrow but hopefully I’ll be able to look at it by Friday unless you get your permission and submit a PR beforehand in which case I’ll try and merge it as soon as it passes CI. Thanks. |
Hm, sounds like I need to cast to UInt32 for the bitfield manipulation, then cast back to Int32 for the rest. Shouldn't be especially difficult. |
Cool. Maybe add code to the test to test it out while you’re at it. 🤔 |
…aaaand I've also added a fix for the orphaned socket descriptors now. That should about wrap everything up. |
Great, I’ll try and do the merge tomorrow morning. Thanks for the quick response. How’s the leg? Feeling better I hope. |
Fixed in 0.12.93. |
If I repeatedly try to open a connection with timeout specified, I eventually crash, with the message "Not enough bits to represent a signed value". The playground I used to isolate this issue looks like this:
Replace the IP with a valid host on the same network, but a port that isn't listening (I used 192.168.43.1 but that'll depend on your network I suppose).
I see the
connect
call block for the full 1000ms and then fail with an error each time, correctly so. After an arbitrary number of iterations (it doesn't seem consistent and I don't know why) it crashes with the error I mentioned above.Here's the output from one run of that playground. I added a line to
connect
to print the value ofsocketDescriptor
, so that's where the extra line per iteration comes from here:If I look at the stack trace, I find myself at this snippet:
https://github.com/IBM-Swift/BlueSocket/blob/57d7e388dfc985a2fc301ed810752b707462294c/Sources/Socket/SocketUtils.swift#L154-L159
We are crashing on the line
let mask = Int32(1 << bitOffset)
, wherebitOffset
is 31. I think that upper bit is a sign bit, so 1 << 31 is out of range for a signed Int32.This reveals what I believe to be two issues:
bitOffset
to be 31 will cause a crash--I think there are a few scenarios where this can happen, perhaps opening multiple connections in parallel?connect(to:port:timeout)
failed with an Error, but the underlying socket file descriptor still exists--it seemsconnect
isn'tclose()
ing the socket it creates when it bails with an Error, in a few spots. I cannotclose()
this myself, as the value ofsocketDescriptor
is only saved if the connection succeeds.Please let me know if I've left out needed details. I would love to contribute a PR, but I need to get permission from our legal team before signing the CLA.
The text was updated successfully, but these errors were encountered: