Skip to content

Commit b5e0495

Browse files
committed
fix: re-entrant Collector threads on musl-based systems.
Signed-off-by: Paulo Vital <[email protected]>
1 parent 284a161 commit b5e0495

File tree

3 files changed

+25
-62
lines changed

3 files changed

+25
-62
lines changed

src/instana/collector/base.py

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
import queue # pylint: disable=import-error
1010
import threading
11+
import time
1112

1213
from instana.log import logger
13-
from instana.util import DictionaryOfStan, every
14+
from instana.util import DictionaryOfStan
1415

1516

1617
class BaseCollector(object):
@@ -76,22 +77,22 @@ def start(self):
7677
"""
7778
if self.is_reporting_thread_running():
7879
if self.thread_shutdown.is_set():
79-
# Shutdown still in progress; Reschedule this start in 5 seconds from now
80+
# Force a restart.
81+
self.thread_shutdown.clear()
82+
# Reschedule this start in 5 seconds from now
8083
timer = threading.Timer(5, self.start)
8184
timer.daemon = True
8285
timer.name = "Collector Timed Start"
8386
timer.start()
8487
return
8588
logger.debug(
86-
"BaseCollector.start non-fatal: call but thread already running (started: %s)",
87-
self.started,
89+
f"BaseCollector.start non-fatal: call but thread already running (started: {self.started})"
8890
)
89-
return
9091

9192
if self.agent.can_send():
9293
logger.debug("BaseCollector.start: launching collection thread")
9394
self.thread_shutdown.clear()
94-
self.reporting_thread = threading.Thread(target=self.thread_loop, args=())
95+
self.reporting_thread = threading.Thread(target=self.background_report, args=())
9596
self.reporting_thread.daemon = True
9697
self.reporting_thread.name = self.THREAD_NAME
9798
self.reporting_thread.start()
@@ -113,37 +114,25 @@ def shutdown(self, report_final=True):
113114
self.prepare_and_report_data()
114115
self.started = False
115116

116-
def thread_loop(self):
117-
"""
118-
Just a loop that is run in the background thread.
119-
@return: None
120-
"""
121-
every(
122-
self.report_interval,
123-
self.background_report,
124-
"Instana Collector: prepare_and_report_data",
125-
)
126-
127-
def background_report(self):
117+
def background_report(self) -> None:
128118
"""
129119
The main work-horse method to report data in the background thread.
130-
@return: Boolean
131-
"""
132-
if self.thread_shutdown.is_set():
133-
logger.debug(
134-
"Thread shutdown signal is active: Shutting down reporting thread"
135-
)
136-
return False
137-
138-
self.prepare_and_report_data()
139120
140-
if self.thread_shutdown.is_set():
141-
logger.debug(
142-
"Thread shutdown signal is active: Shutting down reporting thread"
143-
)
144-
return False
121+
This method runs indefinitely, preparing and reporting data at regular
122+
intervals.
123+
It checks for a shutdown signal and stops execution if it's set.
124+
125+
@return: None
126+
"""
127+
while True:
128+
if self.thread_shutdown.is_set():
129+
logger.debug(
130+
"Thread shutdown signal is active: Shutting down reporting thread"
131+
)
132+
break
145133

146-
return True
134+
self.prepare_and_report_data()
135+
time.sleep(self.report_interval)
147136

148137
def prepare_and_report_data(self):
149138
"""

src/instana/util/__init__.py

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
# (c) Copyright IBM Corp. 2021
22
# (c) Copyright Instana Inc. 2020
33

4+
import importlib.metadata
45
import json
5-
import time
66
from collections import defaultdict
77
from urllib import parse
88

9-
import importlib.metadata
10-
11-
from ..log import logger
9+
from instana.log import logger
1210

1311

1412
def nested_dictionary():
@@ -111,30 +109,6 @@ def get_default_gateway():
111109
logger.warning("get_default_gateway: ", exc_info=True)
112110

113111

114-
def every(delay, task, name):
115-
"""
116-
Executes a task every `delay` seconds
117-
118-
:param delay: the delay in seconds
119-
:param task: the method to run. The method should return False if you want the loop to stop.
120-
:return: None
121-
"""
122-
next_time = time.time() + delay
123-
124-
while True:
125-
time.sleep(max(0, next_time - time.time()))
126-
try:
127-
if task() is False:
128-
break
129-
except Exception:
130-
logger.debug(
131-
"Problem while executing repetitive task: %s", name, exc_info=True
132-
)
133-
134-
# skip tasks if we are behind schedule:
135-
next_time += (time.time() - next_time) // delay * delay + delay
136-
137-
138112
def validate_url(url):
139113
"""
140114
Validate if <url> is a valid url

src/instana/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
# Module version file. Used by setup.py and snapshot reporting.
55

6-
VERSION = "3.4.0"
6+
VERSION = "3.4.0+dev.celero.debug.10"

0 commit comments

Comments
 (0)