Skip to content

Commit 7a7867b

Browse files
authored
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.
1 parent 53c5b9d commit 7a7867b

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

sentry_sdk/client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,15 @@ def _capture_envelope(envelope):
258258
SDK_INFO["name"] = sdk_name
259259
logger.debug("Setting SDK name to '%s'", sdk_name)
260260

261+
if has_profiling_enabled(self.options):
262+
try:
263+
setup_profiler(self.options)
264+
except Exception as e:
265+
logger.debug("Can not set up profiler. (%s)", e)
266+
261267
finally:
262268
_client_init_debug.set(old_debug)
263269

264-
if has_profiling_enabled(self.options):
265-
try:
266-
setup_profiler(self.options)
267-
except ValueError as e:
268-
logger.debug(str(e))
269-
270270
self._setup_instrumentation(self.options.get("functions_to_trace", []))
271271

272272
@property

sentry_sdk/profiler.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,10 @@ def teardown_profiler():
248248
MAX_STACK_DEPTH = 128
249249

250250

251-
CWD = os.getcwd()
252-
253-
254251
def extract_stack(
255252
raw_frame, # type: Optional[FrameType]
256253
cache, # type: LRUCache
257-
cwd=CWD, # type: str
254+
cwd, # type: str
258255
max_stack_depth=MAX_STACK_DEPTH, # type: int
259256
):
260257
# type: (...) -> ExtractedStack

tests/test_profiler.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,10 @@ def test_extract_stack_with_max_depth(depth, max_stack_depth, actual_depth):
500500
# increase the max_depth by the `base_stack_depth` to account
501501
# for the extra frames pytest will add
502502
_, frame_ids, frames = extract_stack(
503-
frame, LRUCache(max_size=1), max_stack_depth=max_stack_depth + base_stack_depth
503+
frame,
504+
LRUCache(max_size=1),
505+
max_stack_depth=max_stack_depth + base_stack_depth,
506+
cwd=os.getcwd(),
504507
)
505508
assert len(frame_ids) == base_stack_depth + actual_depth
506509
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):
527530
def test_extract_stack_with_cache(frame, depth):
528531
# make sure cache has enough room or this test will fail
529532
cache = LRUCache(max_size=depth)
530-
_, _, frames1 = extract_stack(frame, cache)
531-
_, _, frames2 = extract_stack(frame, cache)
533+
cwd = os.getcwd()
534+
_, _, frames1 = extract_stack(frame, cache, cwd=cwd)
535+
_, _, frames2 = extract_stack(frame, cache, cwd=cwd)
532536

533537
assert len(frames1) > 0
534538
assert len(frames2) > 0
@@ -667,7 +671,16 @@ def test_thread_scheduler_single_background_thread(scheduler_class):
667671
)
668672
@mock.patch("sentry_sdk.profiler.MAX_PROFILE_DURATION_NS", 1)
669673
def test_max_profile_duration_reached(scheduler_class):
670-
sample = [("1", extract_stack(get_frame(), LRUCache(max_size=1)))]
674+
sample = [
675+
(
676+
"1",
677+
extract_stack(
678+
get_frame(),
679+
LRUCache(max_size=1),
680+
cwd=os.getcwd(),
681+
),
682+
),
683+
]
671684

672685
with scheduler_class(frequency=1000) as scheduler:
673686
transaction = Transaction(sampled=True)
@@ -711,8 +724,18 @@ def ensure_running(self):
711724

712725

713726
sample_stacks = [
714-
extract_stack(get_frame(), LRUCache(max_size=1), max_stack_depth=1),
715-
extract_stack(get_frame(), LRUCache(max_size=1), max_stack_depth=2),
727+
extract_stack(
728+
get_frame(),
729+
LRUCache(max_size=1),
730+
max_stack_depth=1,
731+
cwd=os.getcwd(),
732+
),
733+
extract_stack(
734+
get_frame(),
735+
LRUCache(max_size=1),
736+
max_stack_depth=2,
737+
cwd=os.getcwd(),
738+
),
716739
]
717740

718741

@@ -805,7 +828,7 @@ def ensure_running(self):
805828
"stacks": [[0], [1, 0]],
806829
"thread_metadata": thread_metadata,
807830
},
808-
id="two identical stacks",
831+
id="two different stacks",
809832
),
810833
],
811834
)

0 commit comments

Comments
 (0)