Skip to content

Commit

Permalink
Fix data races in client connection state logic
Browse files Browse the repository at this point in the history
  • Loading branch information
knowledge4igor committed Jan 1, 2019
1 parent 7fe7bb5 commit e1784bf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions include/pistache/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ struct Connection : public std::enable_shared_from_this<Connection> {

Connection()
: fd(-1)
, requestEntry(nullptr)
, connectionState_(NotConnected)
, requestEntry(nullptr)
{
state_.store(static_cast<uint32_t>(State::Idle));
connectionState_.store(NotConnected);
}

struct RequestData {
Expand Down Expand Up @@ -133,7 +133,7 @@ struct Connection : public std::enable_shared_from_this<Connection> {
struct sockaddr_in saddr;
std::unique_ptr<RequestEntry> requestEntry;
std::atomic<uint32_t> state_;
ConnectionState connectionState_;
std::atomic<ConnectionState> connectionState_;
std::shared_ptr<Transport> transport_;
Queue<RequestData> requestsQueue;

Expand Down
8 changes: 4 additions & 4 deletions src/client/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,14 @@ Connection::connect(const Address& addr)

make_non_blocking(sfd);

connectionState_ = Connecting;
connectionState_.store(Connecting);
fd = sfd;

transport_->asyncConnect(shared_from_this(), addr->ai_addr, addr->ai_addrlen)
.then([=]() {
socklen_t len = sizeof(saddr);
getsockname(sfd, (struct sockaddr *)&saddr, &len);
connectionState_ = Connected;
connectionState_.store(Connected);
processRequestQueue();
}, ExceptionPrinter());
break;
Expand All @@ -388,12 +388,12 @@ Connection::isIdle() const {

bool
Connection::isConnected() const {
return connectionState_ == Connected;
return connectionState_.load() == Connected;
}

void
Connection::close() {
connectionState_ = NotConnected;
connectionState_.store(NotConnected);
::close(fd);
}

Expand Down

0 comments on commit e1784bf

Please sign in to comment.