Skip to content

profiling.sampling is not working for replay mode #148225

Closed

Description

@seanleeee13

Bug report

Bug description:

I ran next command with a.py code, and it made the error.

python3.15 -m profiling.sampling run -o output.prof a.py
python3.15 -m profiling.sampling replay output.prof
# a.py
for i in range(10000):
    a = [*p for p in [[1, 2], [2], [3, 4, 4]]]

This is the error message:

Traceback (most recent call last):
  File "<frozen runpy>", line 196, in _run_module_as_main
  File "<frozen runpy>", line 87, in _run_code
  File "C:\Users\XXX\AppData\Local\Python\pythoncore-3.15-64\Lib\profiling\sampling\__main__.py", line 65, in <module>
    main()
    ~~~~^^
  File "C:\Users\XXX\AppData\Local\Python\pythoncore-3.15-64\Lib\profiling\sampling\cli.py", line 788, in main
    _main()
    ~~~~~^^
  File "C:\Users\XXX\AppData\Local\Python\pythoncore-3.15-64\Lib\profiling\sampling\cli.py", line 922, in _main
    handler(args)
    ~~~~~~~^^^^^^
  File "C:\Users\XXX\AppData\Local\Python\pythoncore-3.15-64\Lib\profiling\sampling\cli.py", line 1175, in _handle_replay
    with BinaryReader(args.input_file) as reader:
         ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "C:\Users\XXX\AppData\Local\Python\pythoncore-3.15-64\Lib\profiling\sampling\binary_reader.py", line 32, in __enter__
    self._reader = _remote_debugging.BinaryReader(self.filename)
                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
ValueError: Invalid magic number: 0xfa03a97b

The reported magic number 0xfa03a97b corresponds to Python 3.13/3.14. It seems that profiling.sampling might be inadvertently picking up components or generating data using the 3.14 environment available on the PATH, even when explicitly invoked with python3.15.
I used python 3.15.0a7, and I also downloaded python 3.14.3, and only 3.14.3 is on PATH.
Environments - Window 11 64bit, Using VSCode powershell terminal, pwsh 7.5.5
Also, I tried to do this in venv but python -m profiling.sampling run -o output.prof a.py made another error.
Failed to find the PyRuntime section in process 15236 on Windows platform

CPython versions tested on:

3.15

Operating systems tested on:

Windows

Linked PRs

Activity

  1. Ignyra commented on Apr 7, 2026

    @Ignyra

    I looked into this a bit, and it seems that the initial error comes from reading the data as the binary format while the original command produced a default pstats output. The replay command only works with binary format files that can be generated using --binary, and it doesn’t validate the input format, so passing a pstats file leads to the confusing error in which the BinaryReader expected a magic number in the header where it is only generated for binary files. I think this should work as expected:

    python -m profiling.sampling run --binary -o output.prof test.py
    python -m profiling.sampling replay output.prof
    
  2. seanleeee13 commented on Apr 7, 2026

    @seanleeee13
    Author

    I tried to do with --binary option, and it made Failed to find the PyRuntime section in process 26260 on Windows platform error, a simmilar error with when using venv.

  3. seanleeee13 commented on Apr 7, 2026

    @seanleeee13
    Author

    Tried with another a.py - and it worked.

    import time
    
    for i in range(1000000):
        a = [p for p in [[1, 2], [2], [3, 4, 4]]]
        if i % 100000 == 0:
            time.sleep(0.1)

    I think it could be an error because a.py is to short for sampling.
    Also, I updated to python 3.15.0a8, but it doesn't work on venv yet.

  4. Ignyra commented on Apr 8, 2026

    @Ignyra

    I tried to reproduce this on Windows with Python 3.15.0a8, but I wasn’t able to hit the same error. It might be related to something environment-specific.

  5. added 3 commits that reference this issue on Apr 8, 2026
  6. pablogsal commented on Apr 8, 2026

    @pablogsal
    Member

    Yeah I think the only bug here that we can validate is that the binary files are not validated before checking and the error is confusing. I will work on a fix

  7. added 2 commits that reference this issue on Apr 8, 2026
  8. pablogsal commented on Apr 9, 2026

    @pablogsal
    Member

    Thanks for the report!

  9. added a commit that references this issue on Apr 25, 2026
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

    stdlibStandard Library Python modules in the Lib/ directorytopic-profilingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions