Skip to content

Uncaught exception when Sentry serializes a stacktrace when CWD was removed #4587

@chodges15

Description

@chodges15

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.33.0

Steps to Reproduce

usage: SENTRY_DSN="https://examplePublicKey@o0.ingest.sentry.io/0" ./reproduce_abspath_bug.sh

#!/usr/bin/env bash
#
# reproduce_abspath_bug.sh
#
# Reproduces the “os.path.abspath → FileNotFoundError” bug
# when the current working directory has been deleted.
# -------------------------------------------------------
# Usage:
#   chmod +x reproduce_abspath_bug.sh
#   ./reproduce_abspath_bug.sh "<YOUR_SENTRY_DSN>"
#
# If you don’t care about actually sending the event,
# you can omit the DSN or pass an empty string.

set -euo pipefail

# -------- configuration --------
PY_VER=python3.10
SDK_VERSION=2.33.0
VENV_DIR="$(mktemp -d)"
DSN="${1:-}"            # first CLI arg or empty
# --------------------------------

echo "Creating virtual environment in $VENV_DIR ..."
$PY_VER -m venv "$VENV_DIR"
# shellcheck disable=SC1090
source "$VENV_DIR/bin/activate"

echo "Installing sentry-sdk==$SDK_VERSION ..."
pip install --no-color --quiet "sentry-sdk==$SDK_VERSION"

echo "Running reproduction snippet ..."
$PY_VER - <<'PY'
import os, shutil, tempfile, sentry_sdk, sys

DSN = os.getenv("SENTRY_DSN", "")
if DSN:
    sentry_sdk.init(dsn=DSN)
else:
    print("⚠️  No DSN provided — event will not be sent to Sentry", file=sys.stderr)

tmp = tempfile.mkdtemp()
os.chdir(tmp)           # change into fresh temp dir
shutil.rmtree(tmp)      # delete it behind our back

try:
    raise RuntimeError("boom")
except Exception as exc:
    print("sending to sentry ...")
    sentry_sdk.capture_exception(exc)
PY

echo
echo "✅  Finished. Check the console output above for the FileNotFoundError traceback."
echo "Cleaning up ..."
deactivate
rm -rf "$VENV_DIR"

Expected Result

Sentry should capture and send the original error without raising a secondary exception.

Actual Result

Instead of the original error, Sentry crashes while serialising the stacktrace:

Traceback (most recent call last):
  File "<stdin>", line 14, in <module>
RuntimeError: boom

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 17, in <module>
  File "/tmp/tmp.3c3lRZYJ4Q/lib/python3.10/site-packages/sentry_sdk/api.py", line 107, in capture_exception
    return Hub.current.capture_exception(error, scope=scope, **scope_args)
  File "/tmp/tmp.3c3lRZYJ4Q/lib/python3.10/site-packages/sentry_sdk/hub.py", line 386, in capture_exception
    event, hint = event_from_exception(exc_info, client_options=client.options)
  File "/tmp/tmp.3c3lRZYJ4Q/lib/python3.10/site-packages/sentry_sdk/utils.py", line 888, in event_from_exception
    "values": exceptions_from_error_tuple(
  File "/tmp/tmp.3c3lRZYJ4Q/lib/python3.10/site-packages/sentry_sdk/utils.py", line 763, in exceptions_from_error_tuple
    single_exception_from_error_tuple(
  File "/tmp/tmp.3c3lRZYJ4Q/lib/python3.10/site-packages/sentry_sdk/utils.py", line 689, in single_exception_from_error_tuple
    frames = [
  File "/tmp/tmp.3c3lRZYJ4Q/lib/python3.10/site-packages/sentry_sdk/utils.py", line 690, in <listcomp>
    serialize_frame(
  File "/tmp/tmp.3c3lRZYJ4Q/lib/python3.10/site-packages/sentry_sdk/utils.py", line 618, in serialize_frame
    "abs_path": os.path.abspath(abs_path) if abs_path else None,
  File "/usr/lib/python3.10/posixpath.py", line 384, in abspath
    cwd = os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory

os.path.abspath calls os.getcwd() when given a relative path; if the current working directory has been deleted, getcwd() raises FileNotFoundError, which bubbles up through Sentry’s serialize_frame. Because the built-in C function getcwd does not create its own Python frame, the traceback points only to posixpath.abspath, making diagnosis confusing.

Metadata

Metadata

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions