Skip to content

Tachyon sample dump/replay crashes #144336

Closed

Description

@hauntsaninja

Bug description:

I'm running python -m profiling.sampling replay prof.bin --heatmap after having captured some profile and it crashes.

When I dropped a breakpoint in I saw the dump somehow has ended up with one instance where stat.filename == '\x00'

Error: Failed to export heatmap: embedded null byte
Traceback (most recent call last):
  File "<frozen runpy>", line 196, in _run_module_as_main
  File "<frozen runpy>", line 87, in _run_code
  File "/.../lib/python3.15/profiling/sampling/__main__.py", line 65, in <module>
    main()
    ~~~~^^
  File "/.../lib/python3.15/profiling/sampling/cli.py", line 788, in main
    _main()
    ~~~~~^^
  File "/.../lib/python3.15/profiling/sampling/cli.py", line 922, in _main
    handler(args)
    ~~~~~~~^^^^^^
  File "/.../lib/python3.15/profiling/sampling/cli.py", line 1206, in _handle_replay
    collector.export(filename)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/.../lib/python3.15/profiling/sampling/heatmap_collector.py", line 725, in export
    self._generate_file_reports(output_dir, file_stats)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../lib/python3.15/profiling/sampling/heatmap_collector.py", line 766, in _generate_file_reports
    self._generate_file_html(
    ~~~~~~~~~~~~~~~~~~~~~~~~^
        file_path,
        ^^^^^^^^^^
    ...<3 lines>...
        stat
        ^^^^
    )
    ^
  File "/.../lib/python3.15/profiling/sampling/heatmap_collector.py", line 905, in _generate_file_html
    source_lines = Path(filename).read_text(encoding='utf-8', errors='replace').splitlines()
                   ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../lib/python3.15/pathlib/__init__.py", line 985, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors, newline=newline) as f:
         ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../lib/python3.15/pathlib/__init__.py", line 969, in open
    return io.open(self, mode, buffering, encoding, errors, newline)
           ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: embedded null byte

Don't yet have a minimal repro, but maybe the issue is apparent to you

CPython versions tested on:

3.15

Operating systems tested on:

macOS

Activity

  1. added
    type-bug
    on Jan 30, 2026
  2. changed the title [-]Tachyon sample dump/replay has issue with bad filename[/-] [+]Tachyon sample dump/replay crashes[/+] on Jan 30, 2026
  3. added
    stdlib
    on Jan 30, 2026
  4. StanFromIreland commented on Jan 30, 2026

    @StanFromIreland
    Member

    Maybe after reading the filename in parse_code_object, we should check the content and not just the length? WDYT @pablogsal

  5. pablogsal commented on Jan 30, 2026

    @pablogsal
    Member

    Don't yet have a minimal repro, but maybe the issue is apparent to you

    Unfortunately not :(

    If you could have a repro or you can send over the file that would be amazing!

  6. pablogsal commented on Jan 30, 2026

    @pablogsal
    Member

    Oh wait this is opening a python file:

      File "/.../lib/python3.15/profiling/sampling/heatmap_collector.py", line 905, in _generate_file_html
        source_lines = Path(filename).read_text(encoding='utf-8', errors='replace').splitlines()
                       ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/.../lib/python3.15/pathlib/__init__.py", line 985, in read_text
        with self.open(mode='r', encoding=encoding, errors=errors, newline=newline) as f:
             ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/.../lib/python3.15/pathlib/__init__.py", line 969, in open
        return io.open(self, mode, buffering, encoding, errors, newline)
               ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ValueError: embedded null byte
    

    It's here:

    source_lines = Path(filename).read_text(encoding='utf-8', errors='replace').splitlines()

    Is not the result file is just a python file!

  7. pablogsal commented on Jan 30, 2026

    @pablogsal
    Member

    Ah damn, ok, the filename comes from stat.filename. Yeah I think i still would like to see at least the file

  8. hauntsaninja commented on Feb 2, 2026

    @hauntsaninja
    ContributorAuthor

    Let me try to figure out a repro or shareable dump.

    In the meantime, here's something maybe useful from the bad file I have:

    >>> collector = CollapsedStackCollector(interval)
    >>> count = reader.replay_samples(collector, None)
    >>> next(k for k in collector.stack_counter if r"\x00" in repr(k))
    ((<many tuples of (filename, lineno, func)>, ('\x00', 0, '`@')), 17992827)
    

    There is a second instance as well. That one is:

    >>> [k for k in collector.stack_counter if r"\x00" in repr(k)][1]
    ((<many tuples of (filename, lineno, func)>, ('\x00', 0, 'P'), (filename, lineno, func)), 17992827)
    
  9. pablogsal commented on Feb 3, 2026

    @pablogsal
    Member

    @hauntsaninja can you try to apply this diff and see if it reproduces?

    diff --git a/Modules/_remote_debugging/object_reading.c b/Modules/_remote_debugging/object_reading.c
    index 447b7fd5926..36503f4be5d 100644
    --- a/Modules/_remote_debugging/object_reading.c
    +++ b/Modules/_remote_debugging/object_reading.c
    @@ -87,6 +87,15 @@ read_py_str(
         }
         buf[len] = '\0';
    
    +    // Reject garbage: valid Python identifier strings (filenames, function names)
    +    // should not contain null bytes. This detects corrupted memory from reading
    +    // stale pointers that point to freed/reallocated memory.
    +    if (memchr(buf, '\0', len) != NULL) {
    +        set_exception_cause(unwinder, PyExc_RuntimeError,
    +            "String contains null byte (likely corrupted memory)");
    +        goto err;
    +    }
    +
         result = PyUnicode_FromStringAndSize(buf, len);
         if (result == NULL) {
             set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create PyUnicode from remote string data");
  10. ivonastojanovic commented on Oct 23, 2026

    @ivonastojanovic
    Contributor

    Closing this one since this was fixed in #158853.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibtopic-profilingtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions