Skip to content

Commit

Permalink
revert previous 2 commits
Browse files Browse the repository at this point in the history
fix: flora agent crash
fix: deadlock during clean gabages""
  • Loading branch information
张晨 authored and 张晨 committed Jul 19, 2019
1 parent 4924e17 commit 4d0940e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
4 changes: 4 additions & 0 deletions include/flora-agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <map>
#include <mutex>
#include <thread>
#include <list>

namespace flora {

Expand Down Expand Up @@ -101,6 +102,8 @@ class Agent : public ClientCallback {

void destroy_client();

void clean_gabages(std::list<std::shared_ptr<Client> >& gabages);

private:
class Options {
public:
Expand All @@ -123,6 +126,7 @@ class Agent : public ClientCallback {
std::condition_variable start_cond;
std::shared_ptr<Client> flora_cli;
std::thread run_thread;
std::mutex cg_mutex;
bool working = false;
};

Expand Down
2 changes: 2 additions & 0 deletions src/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ void Client::iclose(bool passive, int32_t err) {
}

int32_t Client::close(bool passive) {
if (connection == nullptr || connection->closed())
return FLORA_CLI_SUCCESS;
if (this_thread::get_id() == callback_thr_id)
return FLORA_CLI_EDEADLOCK;
iclose(passive, FLORA_CLI_ECLOSED);
Expand Down
28 changes: 25 additions & 3 deletions src/flora-agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,28 @@ void Agent::start(bool block) {
}
}

void Agent::clean_gabages(list<shared_ptr<Client> >& gabages) {
lock_guard<mutex> locker(cg_mutex);
auto it = gabages.begin();
while (it != gabages.end()) {
auto cli = *it;
if (cli) {
static_pointer_cast<flora::internal::Client>(cli)->close(false);
if (cli.use_count() == 1) {
cli.reset();
it = gabages.erase(it);
} else
++it;
} else {
++it;
}
}
}

void Agent::run() {
unique_lock<mutex> locker(conn_mutex, defer_lock);
shared_ptr<Client> cli, gabage;
shared_ptr<Client> cli;
list<shared_ptr<Client> > gabages;
flora::ClientOptions cliopts;

cliopts.bufsize = options.bufsize;
Expand All @@ -138,13 +157,14 @@ void Agent::run() {
KLOGI(TAG, "flora service %s connected", options.uri.c_str());
init_cli(cli);
locker.lock();
gabages.push_back(cli);
flora_cli.swap(cli);
start_cond.notify_one();
conn_cond.wait(locker);
gabage.swap(flora_cli);
flora_cli.reset();
}
locker.unlock();
gabage.reset();
clean_gabages(gabages);
}
}

Expand All @@ -171,10 +191,12 @@ void Agent::close() {
post_handlers.clear();
call_handlers.clear();
locker.unlock();
cg_mutex.lock();
if (cli != nullptr && cli->close(false) == FLORA_CLI_EDEADLOCK) {
thread tmp([cli]() { cli->close(false); });
tmp.detach();
}
cg_mutex.unlock();
if (run_thread.joinable())
run_thread.join();
}
Expand Down
3 changes: 2 additions & 1 deletion src/sock-conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ bool SocketConn::connect(const std::string &host, int32_t port) {

bool SocketConn::send(const void *data, uint32_t size) {
lock_guard<mutex> locker(write_mutex);
if (!sock_ready)
if (!sock_ready) {
return false;
}
ssize_t c = ::write(sock, data, size);
if (c < 0) {
KLOGE(TAG, "write to socket failed: %s", strerror(errno));
Expand Down

0 comments on commit 4d0940e

Please sign in to comment.