Skip to content

Commit

Permalink
fix: flora agent crash
Browse files Browse the repository at this point in the history
prevent destruct flora::Client in flora callback thread
  • Loading branch information
ming committed Jul 18, 2019
1 parent a79cefc commit 0603096
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/flora-agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,27 @@ void Agent::start(bool block) {
}
}

static void clean_gabages(list<shared_ptr<Client> >& gabages) {
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 +156,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 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 0603096

Please sign in to comment.