From 0130bfb8b6ea74086c455a0397c6fb27bec473b1 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Wed, 21 Aug 2019 14:05:00 +0300
Subject: [PATCH] WIP attempt to fix error on disconnect

---
 components/common/cpp/src/timer.cpp      |  6 ++++--
 components/rgbd-sources/src/streamer.cpp | 12 ++++++++++--
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/components/common/cpp/src/timer.cpp b/components/common/cpp/src/timer.cpp
index 23de94761..6064c1f74 100644
--- a/components/common/cpp/src/timer.cpp
+++ b/components/common/cpp/src/timer.cpp
@@ -61,11 +61,13 @@ static void waitTimePoint() {
 	}
 
 	// Still lots of time so do some idle jobs
-	{
+	if (msdelay >= 10 && sincelast != mspf) {
 		UNIQUE_LOCK(mtx, lk);
 		auto idle_job = jobs[kTimerIdle10].begin();
 		while (idle_job != jobs[kTimerIdle10].end() && msdelay >= 10 && sincelast != mspf) {
-			(*idle_job++).job(now);
+			(*idle_job).active = true;
+			(*idle_job).job(now);
+			(*idle_job++).active = false;
 			now = get_time();
 			msdelay = mspf - (now % mspf);
 		}
diff --git a/components/rgbd-sources/src/streamer.cpp b/components/rgbd-sources/src/streamer.cpp
index 962da5a37..311b69021 100644
--- a/components/rgbd-sources/src/streamer.cpp
+++ b/components/rgbd-sources/src/streamer.cpp
@@ -173,10 +173,18 @@ void Streamer::_addClient(const string &source, int N, int rate, const ftl::UUID
 		if (time_peer_ == ftl::UUID(0)) {
 			time_peer_ = peer;
 
+			// Do a time sync whenever the CPU is idle for 10ms or more.
+			// FIXME: Could be starved
 			timer_job_ = ftl::timer::add(ftl::timer::kTimerIdle10, [peer,this](int id) {
-				// Also do a time sync (but should be repeated periodically)
 				auto start = std::chrono::high_resolution_clock::now();
-				int64_t mastertime = net_->call<int64_t>(peer, "__ping__");
+				int64_t mastertime;
+
+				try {
+					mastertime = net_->call<int64_t>(peer, "__ping__");
+				} catch (...) {
+					//timer_job_.cancel();
+				}
+
 				auto elapsed = std::chrono::high_resolution_clock::now() - start;
 				int64_t latency = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
 				auto clock_adjust = mastertime - (ftl::timer::get_time() + (latency/2));
-- 
GitLab