question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

freeze_time might leave some instances patched after exit

See original GitHub issue

Module A gets imported inside freeze_time context manager Module A imports datetime.datetime (patched with freezegun). Context manager exists and unpatches all modules it has tracked in undo_changes.

The problem is that module A still has FakeDatetime instead of datetime.datetime, which results in something like that if it gets called:

    return cls.times_to_freeze[-1]
IndexError: list index out of range

Sorry, I have no test to show this bug.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
GDICommandercommented, Aug 30, 2016

I have the same issue with 0.3.7, I am using a pytest fixture that uses “freeze_time”:

@pytest.yield_fixture
def frozen_clock():
    with freeze_time("2012-01-14 12:00:01.123"):
        yield

One of the tests that does not use this fixture hits this issue when using structlog and datetime.utcnow():

rdv_load_test_agent\tunnel.py:475: in load
    data = await self._send_message('LOAD', path=bpt_file)
C:\Python35\Lib\contextlib.py:77: in __exit__
    self.gen.throw(type, value, traceback)
rdv_load_test_agent\tunnel.py:112: in monitor_transaction
    event_log.info(event, outcome=outcome, duration=duration, **kwds)
..\venvs\rdv_lta_pagagne\lib\site-packages\structlog\_base.py:176: in _proxy_to_logger
    args, kw = self._process_event(method_name, event, event_kw)
..\venvs\rdv_lta_pagagne\lib\site-packages\structlog\_base.py:136: in _process_event
    event_dict = proc(self._logger, method_name, event_dict)
..\venvs\rdv_lta_pagagne\lib\site-packages\structlog\processors.py:213: in stamper
    event_dict[key] = now_method().isoformat() + 'Z'
..\venvs\rdv_lta_pagagne\lib\site-packages\freezegun\api.py:191: in utcnow
    result = cls._time_to_freeze()

cls = <class 'freezegun.api.FakeDatetime'>

    @classmethod
    def _time_to_freeze(cls):
>       return cls.times_to_freeze[-1]()
E       IndexError: list index out of range

If this can help, here is the code in structlog that gets and uses “utcnow”:

        now_method = getattr(datetime.datetime, 'utcnow' if utc else 'now')
        if fmt is None:
            def stamper(self, _, __, event_dict):
                event_dict[key] = calendar.timegm(time.gmtime())
                return event_dict
        elif fmt.upper() == 'ISO':
            if utc:
                def stamper(self, _, __, event_dict):
                    event_dict[key] = now_method().isoformat() + 'Z'
                    return event_dict
0reactions
mindojo-victorcommented, Sep 28, 2016

Here is my attempt to fix this: https://github.com/spulec/freezegun/pull/155

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting Patch Manager - AWS Documentation
Solution 2: Ensure that only one maintenance window at a time is running Run Command tasks ... Permission denied failed to run commands:...
Read more >
Patching EC2 through SSM - Kloud Blog - Telstra Purple
Patch Baseline is configured as follows. Go to Patch Baseline and make the newly created baseline as your default. At this point, the...
Read more >
How micropatching could help close the security update gap
One possible way to decrease time to patch is micropatching -- using a tiny piece of code to fix a single vulnerability, without...
Read more >
Automatic VM Guest Patching for Azure VMs - Microsoft Learn
Learn how to automatically patch virtual machines in Azure. ... The patches can be installed any day on the VM during off-peak hours...
Read more >
FAQs | ManageEngine Patch Manager Plus
Answers to some frequently asked questions in Patch Manager Plus. ... Can I schedule a reboot for a specific time after patches are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found