Skip to content

_remote_debugging: write_sample advances the timestamp before writing the sample #158550

Description

@maurycy

Bug report

(found while thinking about how to address the code review from #157228)

The timestamp is advanced before the stack is built:

https://github.com/python/cpython/blob/b9546455ab0/Modules/_remote_debugging/binary_io_writer.c#L952-L953

If there's a failure, such as:

except (RuntimeError, UnicodeDecodeError, MemoryError, OSError):
flush_pending()
collector.collect_failed_sample()
errors += 1
prev_stack = None

Then the timestamps for the thread is shifted. That's a hard corruption for a delta-based format.

Reproduction

maurycy@gimel cpython-315 (main e580c88?) % cat repro.py
import _remote_debugging

w = _remote_debugging.BinaryWriter("/tmp/o.bin", 1000, 0)
try:
    w.write_sample([(0, [(1, 0, [(42, None, "f", None)])])], 5000)
except TypeError:
    pass
w.write_sample([(0, [(1, 0, [("a.py", None, "f", None)])])], 6000)
w.finalize()

class C:
    def collect(self, frames, timestamps_us=None, **kw):
        print(timestamps_us)

_remote_debugging.BinaryReader("/tmp/o.bin").replay(C())
maurycy@gimel cpython-315 (main e580c88?) % ./python.exe repro.py
[1000]

Another repro could involve _testcapi.set_nomemory, incorrect unicode etc.

CPython versions tested on:

CPython main branch, 3.15

Operating systems tested on:

macOS

Linked PRs

No activity

Activity on this issue will appear here.

Activity

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

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-profilingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions