adamchainz / time-machine

Travel through time in your tests.
MIT License
685 stars 35 forks source link

Segmentation fault in `time_machine_travel(timedelta())` #431

Closed marcinsulikowski closed 5 months ago

marcinsulikowski commented 6 months ago

Python Version

3.12

pytest Version

No response

Package Version

2.14.0

Description

The following Python script:

from datetime import timedelta
import time_machine

with time_machine.travel(timedelta()):
    pass

crashes due to a segmentation fault in the constructor of the travel class.

The crash happens when the constructor tries to determine the destination timestamp like this:

    timestamp = time() + dest.total_seconds()

where time() calls result = _time_machine.original_time() which crashes if time_machine's patches are not applied yet and thus the original_time function pointer is still NULL.

marcinsulikowski commented 6 months ago

I posted PR #432 which should fix this crash.

adamchainz commented 5 months ago

Thank you for the clear report and PR. I didn’t like the change you proposed though, because it would be best to keep calling travel() side-effect free. I would rather time-machine didn’t patch modules until needed, to reduce its overhead and the chance of bugs in processes that import but don’t run tests.

I went with an alternative approach in #433. Note that I included a test that uses a subprocess, which reproduces the issue before the patch.

Released in 2.14.1.

marcinsulikowski commented 5 months ago

Thank you for fixing this so quickly :+1:

adamchainz commented 5 months ago

Thank you for the clear report. As you may see above, a segfault was reported several times but I never could track down the cause.

I tooted and tweeted about your report :) https://fosstodon.org/@adamchainz/112144774490159195