Skip to content

Commit

Permalink
skip files that are already mapped for enhanced stack traces (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
twin-drill authored Jul 9, 2024
1 parent 38d9eef commit 530cadf
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions temporalio/worker/_workflow_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -1870,28 +1870,36 @@ def _enhanced_stack_trace(self) -> temporalio.api.sdk.v1.EnhancedStackTrace:

# this is to use `open`
with temporalio.workflow.unsafe.sandbox_unrestricted():
sources = dict()
stacks = []
sources: Dict[str, temporalio.api.sdk.v1.StackTraceFileSlice] = dict()
stacks: List[temporalio.api.sdk.v1.StackTrace] = []

# future TODO
# site package filter list -- we want to filter out traces from Python's internals and our sdk's internals. This is what `internal_code` is for, but right now it's just set to false.

for task in list(self._tasks):
locations = []
locations: List[temporalio.api.sdk.v1.StackTraceFileLocation] = []

for frame in task.get_stack():
filename = frame.f_code.co_filename
line_number = frame.f_lineno
func_name = frame.f_code.co_name

try:
with open(filename, "r") as f:
code = f.read()
except OSError as ose:
code = f"Cannot access code.\n---\n{ose.strerror}"
# TODO possibly include sentinel/property for success of src scrape? work out with ui
except Exception:
code = f"Generic Error.\n\n{traceback.format_exc()}"

file_slice = temporalio.api.sdk.v1.StackTraceFileSlice(
line_offset=0, content=code
)
if filename not in sources.keys():
try:
with open(filename, "r") as f:
code = f.read()
except OSError as ose:
code = f"Cannot access code.\n---\n{ose.strerror}"
# TODO possibly include sentinel/property for success of src scrape? work out with ui
except Exception:
code = f"Generic Error.\n\n{traceback.format_exc()}"

file_slice = temporalio.api.sdk.v1.StackTraceFileSlice(
line_offset=0, content=code
)

sources[filename] = file_slice

file_location = temporalio.api.sdk.v1.StackTraceFileLocation(
file_path=filename,
line=line_number,
Expand All @@ -1900,7 +1908,6 @@ def _enhanced_stack_trace(self) -> temporalio.api.sdk.v1.EnhancedStackTrace:
internal_code=False,
)

sources[filename] = file_slice
locations.append(file_location)

stacks.append(temporalio.api.sdk.v1.StackTrace(locations=locations))
Expand Down

0 comments on commit 530cadf

Please sign in to comment.