Skip to content

Commit 03bd96d

Browse files
authored
move begin/commit into try/catch block (zerotier#1932)
Thread was exiting in some cases
1 parent 4a85cf5 commit 03bd96d

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

controller/PostgreSQL.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,8 +1011,6 @@ void PostgreSQL::heartbeat()
10111011
int64_t ts = OSUtils::now();
10121012

10131013
if(c->c) {
1014-
pqxx::work w{*c->c};
1015-
10161014
std::string major = std::to_string(ZEROTIER_ONE_VERSION_MAJOR);
10171015
std::string minor = std::to_string(ZEROTIER_ONE_VERSION_MINOR);
10181016
std::string rev = std::to_string(ZEROTIER_ONE_VERSION_REVISION);
@@ -1023,21 +1021,25 @@ void PostgreSQL::heartbeat()
10231021
std::string redis_mem_status = (_redisMemberStatus) ? "true" : "false";
10241022

10251023
try {
1026-
pqxx::result res = w.exec0("INSERT INTO ztc_controller (id, cluster_host, last_alive, public_identity, v_major, v_minor, v_rev, v_build, host_port, use_redis, redis_member_status) "
1027-
"VALUES ("+w.quote(controllerId)+", "+w.quote(hostname)+", TO_TIMESTAMP("+now+"::double precision/1000), "+
1028-
w.quote(publicIdentity)+", "+major+", "+minor+", "+rev+", "+build+", "+host_port+", "+use_redis+", "+redis_mem_status+") "
1029-
"ON CONFLICT (id) DO UPDATE SET cluster_host = EXCLUDED.cluster_host, last_alive = EXCLUDED.last_alive, "
1030-
"public_identity = EXCLUDED.public_identity, v_major = EXCLUDED.v_major, v_minor = EXCLUDED.v_minor, "
1031-
"v_rev = EXCLUDED.v_rev, v_build = EXCLUDED.v_rev, host_port = EXCLUDED.host_port, "
1032-
"use_redis = EXCLUDED.use_redis, redis_member_status = EXCLUDED.redis_member_status");
1024+
pqxx::work w{*c->c};
1025+
1026+
pqxx::result res =
1027+
w.exec0("INSERT INTO ztc_controller (id, cluster_host, last_alive, public_identity, v_major, v_minor, v_rev, v_build, host_port, use_redis, redis_member_status) "
1028+
"VALUES ("+w.quote(controllerId)+", "+w.quote(hostname)+", TO_TIMESTAMP("+now+"::double precision/1000), "+
1029+
w.quote(publicIdentity)+", "+major+", "+minor+", "+rev+", "+build+", "+host_port+", "+use_redis+", "+redis_mem_status+") "
1030+
"ON CONFLICT (id) DO UPDATE SET cluster_host = EXCLUDED.cluster_host, last_alive = EXCLUDED.last_alive, "
1031+
"public_identity = EXCLUDED.public_identity, v_major = EXCLUDED.v_major, v_minor = EXCLUDED.v_minor, "
1032+
"v_rev = EXCLUDED.v_rev, v_build = EXCLUDED.v_rev, host_port = EXCLUDED.host_port, "
1033+
"use_redis = EXCLUDED.use_redis, redis_member_status = EXCLUDED.redis_member_status");
1034+
w.commit();
10331035
} catch (std::exception &e) {
1034-
fprintf(stderr, "Heartbeat update failed: %s\n", e.what());
1036+
fprintf(stderr, "%s: Heartbeat update failed: %s\n", controllerId, e.what());
10351037
w.abort();
10361038
_pool->unborrow(c);
10371039
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
10381040
continue;
1039-
}
1040-
w.commit();
1041+
}
1042+
10411043
}
10421044
_pool->unborrow(c);
10431045

0 commit comments

Comments
 (0)