-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Revert "Revert "Deflakey test advanced 9"" #35091
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,6 +183,41 @@ void ServerConnection::ReadBufferAsync( | |
}); | ||
} | ||
} | ||
void ServerConnection::AsyncWaitTerminated(std::function<void()> callback) { | ||
// Different platforms have different behavior for sockets in asio | ||
// Wait for error and read until the end. | ||
if (status_ == ConnectionStatus::RUNNING) { | ||
socket_.async_wait(local_stream_socket::wait_type::wait_error, | ||
[this, callback](auto) { | ||
if (status_ != ConnectionStatus::TERMINATED) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: Isn't it possible this is returned "before" the connection is actually closed? It seems like this can return when it is simply ready to read the socket. |
||
callback(); | ||
// Close the connection so it'll cancel all other operations | ||
Close(); | ||
} | ||
}); | ||
status_ = ConnectionStatus::TERMINATING; | ||
} | ||
|
||
if (status_ == ConnectionStatus::TERMINATING) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I don't know if I understand this part correctly. I have a couple questions here;
|
||
// This is only used for buffer. Never read. So race condition is | ||
// not an issue here. | ||
static char buffer[1024]; | ||
auto read_cb = [this, cb = callback](boost::system::error_code ec, | ||
std::size_t /* length */) mutable { | ||
if (ec) { | ||
if (status_ != ConnectionStatus::TERMINATED) { | ||
cb(); | ||
// Close the connection so it'll cancel all other operations | ||
Close(); | ||
} | ||
} else { | ||
AsyncWaitTerminated(std::move(cb)); | ||
} | ||
}; | ||
socket_.async_read_some(boost::asio::mutable_buffer(buffer, sizeof(buffer)), | ||
std::move(read_cb)); | ||
} | ||
} | ||
|
||
ray::Status ServerConnection::WriteMessage(int64_t type, | ||
int64_t length, | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -111,6 +111,7 @@ class ServerConnection : public std::enable_shared_from_this<ServerConnection> { | |||||
void Close() { | ||||||
boost::system::error_code ec; | ||||||
socket_.close(ec); | ||||||
status_ = ConnectionStatus::TERMINATED; | ||||||
} | ||||||
|
||||||
/// Get the native handle of the socket. | ||||||
|
@@ -125,11 +126,7 @@ class ServerConnection : public std::enable_shared_from_this<ServerConnection> { | |||||
|
||||||
std::string DebugString() const; | ||||||
|
||||||
void AsyncWaitTerminated(std::function<void()> callback) { | ||||||
// Async wait until the connection is disconnected. | ||||||
socket_.async_wait(local_stream_socket::wait_type::wait_error, | ||||||
[callback = std::move(callback)](auto) { callback(); }); | ||||||
} | ||||||
void AsyncWaitTerminated(std::function<void()> callback); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a docstring? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, we are not closing it actively I believe. It's waiting until bad thing happened passivately. Maybe we shouldn't include close there? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. I thought we close it because we have Close() method inside this Wait method (after async_wait). |
||||||
|
||||||
protected: | ||||||
/// A private constructor for a server connection. | ||||||
|
@@ -144,6 +141,11 @@ class ServerConnection : public std::enable_shared_from_this<ServerConnection> { | |||||
std::function<void(const ray::Status &)> handler; | ||||||
}; | ||||||
|
||||||
enum struct ConnectionStatus { RUNNING = 0, TERMINATING, TERMINATED }; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
just personal preference haha.. |
||||||
|
||||||
/// Whether the connection is terminating. | ||||||
ConnectionStatus status_ = ConnectionStatus::RUNNING; | ||||||
|
||||||
/// The socket connection to the server. | ||||||
local_stream_socket socket_; | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we write unit tests for this method? Seems like it is worth testing it (the logic is pretty complicated).