From 7a7867b2fe0ad8b3d7aeea778b2992b1c506509d Mon Sep 17 00:00:00 2001 From: Tony Xiao Date: Wed, 30 Aug 2023 03:38:21 -0400 Subject: [PATCH] fix(profiler): Do not call getcwd from module root (#2329) * fix(profiler): Do not call getcwd from module root When calling sentry from a cleaned up path, it should not cause an error. So defer the `os.getcwd()` call until later. Fixes #2324. --- sentry_sdk/client.py | 12 ++++++------ sentry_sdk/profiler.py | 5 +---- tests/test_profiler.py | 37 ++++++++++++++++++++++++++++++------- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/sentry_sdk/client.py b/sentry_sdk/client.py index 1a4b044abe..3850b8ec2c 100644 --- a/sentry_sdk/client.py +++ b/sentry_sdk/client.py @@ -258,15 +258,15 @@ def _capture_envelope(envelope): SDK_INFO["name"] = sdk_name logger.debug("Setting SDK name to '%s'", sdk_name) + if has_profiling_enabled(self.options): + try: + setup_profiler(self.options) + except Exception as e: + logger.debug("Can not set up profiler. (%s)", e) + finally: _client_init_debug.set(old_debug) - if has_profiling_enabled(self.options): - try: - setup_profiler(self.options) - except ValueError as e: - logger.debug(str(e)) - self._setup_instrumentation(self.options.get("functions_to_trace", [])) @property diff --git a/sentry_sdk/profiler.py b/sentry_sdk/profiler.py index edc4fc750d..7ae73b056e 100644 --- a/sentry_sdk/profiler.py +++ b/sentry_sdk/profiler.py @@ -248,13 +248,10 @@ def teardown_profiler(): MAX_STACK_DEPTH = 128 -CWD = os.getcwd() - - def extract_stack( raw_frame, # type: Optional[FrameType] cache, # type: LRUCache - cwd=CWD, # type: str + cwd, # type: str max_stack_depth=MAX_STACK_DEPTH, # type: int ): # type: (...) -> ExtractedStack diff --git a/tests/test_profiler.py b/tests/test_profiler.py index 70110e19ce..451ebe65a3 100644 --- a/tests/test_profiler.py +++ b/tests/test_profiler.py @@ -500,7 +500,10 @@ def test_extract_stack_with_max_depth(depth, max_stack_depth, actual_depth): # increase the max_depth by the `base_stack_depth` to account # for the extra frames pytest will add _, frame_ids, frames = extract_stack( - frame, LRUCache(max_size=1), max_stack_depth=max_stack_depth + base_stack_depth + frame, + LRUCache(max_size=1), + max_stack_depth=max_stack_depth + base_stack_depth, + cwd=os.getcwd(), ) assert len(frame_ids) == base_stack_depth + actual_depth assert len(frames) == base_stack_depth + actual_depth @@ -527,8 +530,9 @@ def test_extract_stack_with_max_depth(depth, max_stack_depth, actual_depth): def test_extract_stack_with_cache(frame, depth): # make sure cache has enough room or this test will fail cache = LRUCache(max_size=depth) - _, _, frames1 = extract_stack(frame, cache) - _, _, frames2 = extract_stack(frame, cache) + cwd = os.getcwd() + _, _, frames1 = extract_stack(frame, cache, cwd=cwd) + _, _, frames2 = extract_stack(frame, cache, cwd=cwd) assert len(frames1) > 0 assert len(frames2) > 0 @@ -667,7 +671,16 @@ def test_thread_scheduler_single_background_thread(scheduler_class): ) @mock.patch("sentry_sdk.profiler.MAX_PROFILE_DURATION_NS", 1) def test_max_profile_duration_reached(scheduler_class): - sample = [("1", extract_stack(get_frame(), LRUCache(max_size=1)))] + sample = [ + ( + "1", + extract_stack( + get_frame(), + LRUCache(max_size=1), + cwd=os.getcwd(), + ), + ), + ] with scheduler_class(frequency=1000) as scheduler: transaction = Transaction(sampled=True) @@ -711,8 +724,18 @@ def ensure_running(self): sample_stacks = [ - extract_stack(get_frame(), LRUCache(max_size=1), max_stack_depth=1), - extract_stack(get_frame(), LRUCache(max_size=1), max_stack_depth=2), + extract_stack( + get_frame(), + LRUCache(max_size=1), + max_stack_depth=1, + cwd=os.getcwd(), + ), + extract_stack( + get_frame(), + LRUCache(max_size=1), + max_stack_depth=2, + cwd=os.getcwd(), + ), ] @@ -805,7 +828,7 @@ def ensure_running(self): "stacks": [[0], [1, 0]], "thread_metadata": thread_metadata, }, - id="two identical stacks", + id="two different stacks", ), ], )