Skip to content

next_interval in cpu_signal_handler is not initialized on Windows #947

@Ext3h

Description

@Ext3h

Describe the bug
In cpu_signal_handler, the final Scalene.__signal_manager.restart_timer(next_interval) clause can be reached without next_interval ever having been assigned.

This appears to happen i.e. during exception handling before the first scheduled sample interval as it will then just immediately return and therefor run into the finally clause:

if (
                Scalene.__last_signal_time.virtual == 0
                or Scalene.__last_signal_time.wallclock == 0
            ):
                # Initialization: store values and update on the next pass.
                Scalene.__last_signal_time = now
                if sys.platform != "win32":
                    next_interval = Scalene.sample_cpu_interval()
                    Scalene.__signal_manager.restart_timer(next_interval)
                return

To Reproduce
With package conan===1.66.0 installed via pip:

from importlib.metadata import entry_points
from scalene import scalene_profiler

scalene_profiler.start()
(conan,) = entry_points(group='console_scripts', name='conan')
$ scalene conan_profile.py
Error in program being profiled:
 cannot access local variable 'next_interval' where it is not associated with a value
Traceback (most recent call last):
  File "C:\Users\ar\AppData\Local\Programs\Python\Python313\Lib\importlib\metadata\__init__.py", line 915, in read_text
    return self._path.joinpath(filename).read_text(encoding='utf-8')
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "C:\Users\ar\AppData\Local\Programs\Python\Python313\Lib\pathlib\_local.py", line 546, in read_text
    return PathBase.read_text(self, encoding, errors, newline)
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ar\AppData\Local\Programs\Python\Python313\Lib\pathlib\_abc.py", line 632, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors, newline=newline) as f:
         ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ar\AppData\Local\Programs\Python\Python313\Lib\pathlib\_local.py", line 537, in open
    return io.open(self, mode, buffering, encoding, errors, newline)
           ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\ar\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\\pydantic_core-2.33.2.dist-info\\entry_points.txt'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\ar\AppData\Local\Programs\Python\Python313\Lib\site-packages\scalene\scalene_profiler.py", line 1362, in profile_code
    exec(code, the_globals, the_locals)
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\repositories\conan_profile.py", line 5, in <module>
    (conan,) = entry_points(group='console_scripts', name='conan')
               ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ar\AppData\Local\Programs\Python\Python313\Lib\importlib\metadata\__init__.py", line 1011, in entry_points
    return EntryPoints(eps).select(**params)
           ~~~~~~~~~~~^^^^^
  File "C:\Users\ar\AppData\Local\Programs\Python\Python313\Lib\importlib\metadata\__init__.py", line 1009, in <genexpr>
    dist.entry_points for dist in _unique(distributions())
    ^^^^^^^^^^^^^^^^^
  File "C:\Users\ar\AppData\Local\Programs\Python\Python313\Lib\importlib\metadata\__init__.py", line 496, in entry_points
    return EntryPoints._from_text_for(self.read_text('entry_points.txt'), self)
                                      ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ar\AppData\Local\Programs\Python\Python313\Lib\importlib\metadata\__init__.py", line 908, in read_text
    with suppress(
         ~~~~~~~~^
        FileNotFoundError,
        ^^^^^^^^^^^^^^^^^^
    ...<3 lines>...
        PermissionError,
        ^^^^^^^^^^^^^^^^
    ):
    ^
  File "C:\Users\ar\AppData\Local\Programs\Python\Python313\Lib\contextlib.py", line 450, in __exit__
    def __exit__(self, exctype, excinst, exctb):

  File "C:\Users\ar\AppData\Local\Programs\Python\Python313\Lib\site-packages\scalene\scalene_profiler.py", line 743, in cpu_signal_handler
    Scalene.__signal_manager.restart_timer(next_interval)
                                           ^^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'next_interval' where it is not associated with a value

Expected behavior
Properly initialize the variable if you intend to use it in the final clause?...

Desktop (please complete the following information):

  • OS: Windows 10
  • Reproduced with CPython 3.13.x, 3.10.x

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions