python / python/cpython

Associate thread-local data/context with a faulthandler traceback

Open
#98,825 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type-feature
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

I’d like to be able to associate a thread-local string with the faulthandler module.

Pitch

The faulthandler module is invaluable for tracking down segfaults in native code, however it is really lacking the ability to add some kind of useful breadcumb to aide debugging. Imagine you are running a large-scale distributed job over tens of millions of images and a single one causes opencv to segfault reliably. This can be very difficult to track down.

It would be very useful to be able to add a string (limited by size) via the faulthandler module that is outputted along with the traceback. For example:

for image in millions_of_images():
    faulthandler.context(f'{image.id=}')
    segfaulty_function(image)

# Or maybe:
for image in millions_of_images():
    with faulthandler.context(f'{image.id=}'):
        segfaulty_function(image)

The traceback might be something like:

Fatal Python error: Segmentation fault

Current thread 0x00007fb899f39700 (most recent call first):
  .... (traceback)
Segmentation fault
Context: image_id=foo

You could of course add logging to this function to print out the image ID before you run it through segfaulty_function, but if you're running this at high volume on a huge number of machines, this becomes a bit of an overhead, and it doesn't handle multiple threads running the function well.

Previous discussion

This is lifted from the following thread on Python-ideas: https://mail.python.org/archives/list/python-ideas@python.org/message/BG36W24KEEX4YJA7CZ3PGW4ISZGVXMH6/

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the faulthandler module and review the linked Python-ideas discussion to understand the proposed thread-local context and its constraints. Compare the function and context-manager examples with the requested traceback output; done would require an agreed API and behavior for associating and emitting the context.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.