fidelity / spock

spock is a framework that helps manage complex parameter configurations during research and development of Python applications
https://fidelity.github.io/spock/
Apache License 2.0
121 stars 13 forks source link

Invalid handle with SpockBuilder.save #296

Closed kevindckr closed 9 months ago

kevindckr commented 1 year ago

I have started a simple tutorial for my use case and exploring what spock offers.

from typing import List

from spock import spock, SpockBuilder, SavePath

@spock
class ConfigPath:
    """
    Configuration definition for handling of all paths in project.

    Attributes:
        save_path: where the loaded configuation file is saved
        data_dir: the main data directory
        output_dir: the main output directory
        config_dir: the main configs directory
    """
    save_path: SavePath
    data_dir: str
    output_dir: str
    config_dir: str

if __name__ == "__main__":
    # config = SpockBuilder(ConfigPath, desc="Example configuration for paths").generate()
    # print(config)

    config = SpockBuilder(ConfigPath, desc="Example configuration for paths").save(file_extension='.json').generate()
    print(config)

The corresponding configuration file I have create is the following

{
  "save_path": "C:\\example_project\\configs",
  "data_dir": "C:\\",
  "output_dir": "C:\\",
  "config_dir": "C:\\"
}

When ran with the command python <...>\config_path.py --config paths.json I receive the following exception:

Exception ignored in: <function Popen.__del__ at 0x00000258854A5550>
Traceback (most recent call last):
  File "C:\Users\kdecker\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 949, in __del__
    self._internal_poll(_deadstate=_maxsize)
  File "C:\Users\kdecker\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 1348, in _internal_poll
    if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
OSError: [WinError 6] The handle is invalid

Though this does result in, from what I can tell, a correctly saved .toml file. It contains a header with various info about the machine, python, and git state, the [ConfigPath] parameters themselves, and a footer with the various python libraries in my environment.

This issue does not happen when not calling .save, I load the configuration file into a namespace and print it. This is all under Windows 10 system. Using spock-config 3.0.2.

ncilfone commented 1 year ago

Hi!

Thanks for all the info. I've never tested on windows only osx and linux based systems (which is noted in the installation docs somewhere but it's def not front and center enough). So I guess thanks for being a beta tester!

My first guess is that the error is occurring when spock tries to get the git info of the directory you are working out of to append to the serialized configuration file. It uses a subprocess to call out to git see here: https://github.com/fidelity/spock/blob/1ee250301e9ce6638fb71cec31747b3fb35abb23/spock/utils.py#L602

So it looks like something is going wonky with exiting/killing the subprocess cleanly (as indicated by the __del__ traceback)

Short term band-aid - you can set the flag within the save call to turn off writing the extra_info to the configuration file (https://fidelity.github.io/spock/reference/builder#save). e.g.

SpockBuilder(SpockClass).save(extra_info=False).generate()

This is just extra info that isn't critical to serialization of the actual spock config so everything should still work 100% normally.

I'll try and see if I can figure out what the problem is with window not existing the subprocess correctly.

Nick