Skip to content

Commit

Permalink
fix(profiler): Do not call getcwd from module root (#2329)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Zylphrex authored Aug 30, 2023
1 parent 53c5b9d commit 7a7867b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
12 changes: 6 additions & 6 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions sentry_sdk/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 30 additions & 7 deletions tests/test_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(),
),
]


Expand Down Expand Up @@ -805,7 +828,7 @@ def ensure_running(self):
"stacks": [[0], [1, 0]],
"thread_metadata": thread_metadata,
},
id="two identical stacks",
id="two different stacks",
),
],
)
Expand Down

0 comments on commit 7a7867b

Please sign in to comment.