Commit e1784bf6 authored by knowledge4igor's avatar knowledge4igor

Fix data races in client connection state logic

parent 7fe7bb56
...@@ -40,10 +40,10 @@ struct Connection : public std::enable_shared_from_this<Connection> { ...@@ -40,10 +40,10 @@ struct Connection : public std::enable_shared_from_this<Connection> {
Connection() Connection()
: fd(-1) : fd(-1)
, requestEntry(nullptr) , requestEntry(nullptr)
, connectionState_(NotConnected)
{ {
state_.store(static_cast<uint32_t>(State::Idle)); state_.store(static_cast<uint32_t>(State::Idle));
connectionState_.store(NotConnected);
} }
struct RequestData { struct RequestData {
...@@ -133,7 +133,7 @@ private: ...@@ -133,7 +133,7 @@ private:
struct sockaddr_in saddr; struct sockaddr_in saddr;
std::unique_ptr<RequestEntry> requestEntry; std::unique_ptr<RequestEntry> requestEntry;
std::atomic<uint32_t> state_; std::atomic<uint32_t> state_;
ConnectionState connectionState_; std::atomic<ConnectionState> connectionState_;
std::shared_ptr<Transport> transport_; std::shared_ptr<Transport> transport_;
Queue<RequestData> requestsQueue; Queue<RequestData> requestsQueue;
......
...@@ -355,14 +355,14 @@ Connection::connect(const Address& addr) ...@@ -355,14 +355,14 @@ Connection::connect(const Address& addr)
make_non_blocking(sfd); make_non_blocking(sfd);
connectionState_ = Connecting; connectionState_.store(Connecting);
fd = sfd; fd = sfd;
transport_->asyncConnect(shared_from_this(), addr->ai_addr, addr->ai_addrlen) transport_->asyncConnect(shared_from_this(), addr->ai_addr, addr->ai_addrlen)
.then([=]() { .then([=]() {
socklen_t len = sizeof(saddr); socklen_t len = sizeof(saddr);
getsockname(sfd, (struct sockaddr *)&saddr, &len); getsockname(sfd, (struct sockaddr *)&saddr, &len);
connectionState_ = Connected; connectionState_.store(Connected);
processRequestQueue(); processRequestQueue();
}, ExceptionPrinter()); }, ExceptionPrinter());
break; break;
...@@ -388,12 +388,12 @@ Connection::isIdle() const { ...@@ -388,12 +388,12 @@ Connection::isIdle() const {
bool bool
Connection::isConnected() const { Connection::isConnected() const {
return connectionState_ == Connected; return connectionState_.load() == Connected;
} }
void void
Connection::close() { Connection::close() {
connectionState_ = NotConnected; connectionState_.store(NotConnected);
::close(fd); ::close(fd);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment