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

internal/sockstate: Fix sockets ending up in blocking mode when we monitor them for disconnects. #1311

Merged
merged 2 commits into from
Oct 11, 2022

Conversation

reltuk
Copy link
Contributor

@reltuk reltuk commented Oct 6, 2022

As documented in os, calling *os.File.Fd() will put the socket into blocking mode, makes SetDeadline methods stop working, etc. This syscall to SetNonblocking restores desired functionality.

This was discovered when testing clustering control plane operations in Dolt, which rely on the timely ability to terminate all client connections by calling Close() on them.

We were able to reproduce the issue on macOS by doing the same File() behavior there, despite not having an implementation to actually use the fd for external monitoring of the connection state. To keep as much behavioral parity going forward, I left the fd translation in, since we've observed that it can radically change behavior.

reltuk and others added 2 commits October 5, 2022 21:43
…nitor them for disconnects.

As documented in `os`, calling *os.File.Fd() will put the socket into blocking
mode, makes SetDeadline methods stop working, etc. This syscall to
SetNonblocking restores desired functionality.

This was discovered when testing clustering control plane operations in Dolt,
which rely on the timely ability to terminate all client connections by calling
Close() on them.

We were able to reproduce the issue on macOS by doing the same File() behavior
there, despite not having an implementation to actually use the fd for external
monitoring of the connection state. To keep as much behavioral parity going
forward, I left the fd translation in, since we've observed that it can
radically change behavior.
Copy link
Member

@zachmu zachmu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, one comment

}

defer f.Close()
if isWSL || isProcBlocked {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this check still belongs above the one above, no? Otherwise we get a less useful error message on WSL

I can test this a bit later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, surely TCPConn.File() on WSL doesn't fail? I was trying to mimic the behavior, same as on macOS, where we ensure the ofile got put back into non-blocking.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad I tested this, it's definitely broken on WSL!

Digging into it, will have some patches a bit later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind! Turns out this was hitting a different unrelated bug, this appears to work fine!

Copy link
Member

@zachmu zachmu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on WSL, works great. Sockets do appear to work with the mysql client in that environment, as well as TCP connections.

@reltuk reltuk merged commit bea6a2c into main Oct 11, 2022
@Hydrocharged Hydrocharged deleted the aaron/fix-monitored-tcpconns-blocking-on-linux branch October 13, 2022 12:50
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

Successfully merging this pull request may close these issues.

None yet

2 participants