packetcoders/aerleon_acl_gen_demo

Multiprocessing EOF error with build_acl.py

Opened this issue · 0 comments

Thank you for the blog and demo code !

Multiprocessing reports an EOF error in the build_acl.py.

I am not a programmer, but keen network engineer learning python and modules for networks.

Looks like Multiprocessing doesn't like the functions of the API being called before the main function test

On a Mac with python3.11, demo installed with git and poetry I get:

python3 build_acl.py
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/spawn.py", line 120, in spawn_main
    exitcode = _main(fd, parent_sentinel)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/spawn.py", line 129, in _main
    prepare(preparation_data)
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/spawn.py", line 240, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/spawn.py", line 291, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen runpy>", line 291, in run_path
  File "<frozen runpy>", line 98, in _run_module_code
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/steveh/dev/ip-tools/aerleon_acl_gen_demo/build_acl.py", line 107, in <module>
    configs = api.Generate([cisco_asa_policy], definitions)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/steveh/Library/Caches/pypoetry/virtualenvs/aerleon-demo-zxD1BcZm-py3.11/lib/python3.11/site-packages/aerleon/api.py", line 233, in Generate
    return _Generate(
           ^^^^^^^^^^
  File "/Users/steveh/Library/Caches/pypoetry/virtualenvs/aerleon-demo-zxD1BcZm-py3.11/lib/python3.11/site-packages/aerleon/api.py", line 256, in _Generate
    manager: multiprocessing.managers.SyncManager = context.Manager()
                                                    ^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/context.py", line 57, in Manager
    m.start()
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/managers.py", line 563, in start
    self._process.start()
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
                  ^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/context.py", line 288, in _Popen
    return Popen(process_obj)
           ^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/popen_spawn_posix.py", line 42, in _launch
    prep_data = spawn.get_preparation_data(process_obj._name)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/spawn.py", line 158, in get_preparation_data
    _check_not_importing_main()
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/spawn.py", line 138, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
Traceback (most recent call last):
  File "/Users/steveh/dev/ip-tools/aerleon_acl_gen_demo/build_acl.py", line 107, in <module>
    configs = api.Generate([cisco_asa_policy], definitions)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/steveh/Library/Caches/pypoetry/virtualenvs/aerleon-demo-zxD1BcZm-py3.11/lib/python3.11/site-packages/aerleon/api.py", line 233, in Generate
    return _Generate(
           ^^^^^^^^^^
  File "/Users/steveh/Library/Caches/pypoetry/virtualenvs/aerleon-demo-zxD1BcZm-py3.11/lib/python3.11/site-packages/aerleon/api.py", line 256, in _Generate
    manager: multiprocessing.managers.SyncManager = context.Manager()
                                                    ^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/context.py", line 57, in Manager
    m.start()
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/managers.py", line 567, in start
    self._address = reader.recv()
                    ^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/connection.py", line 249, in recv
    buf = self._recv_bytes()
          ^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/connection.py", line 413, in _recv_bytes
    buf = self._recv(4)
          ^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/connection.py", line 382, in _recv
    raise EOFError
EOFError

Wrapping the API functions in a function and then calling from the main function solves the problem.

...
def makeAsaAcl():
    # Create an instance of the Naming class
    definitions = naming.Naming()

    # Use the ParseDefinitionsObject method to parse the "networks" object
    definitions.ParseDefinitionsObject(networks, "networks")

    # Use the ParseDefinitionsObject method to parse the "services" object
    definitions.ParseDefinitionsObject(services, "services")

    # Use the Generate method from the api module to generate configurations
    # from the "cisco_asa_policy" object, passing in the definitions object
    # as an argument
    configs = api.Generate([cisco_asa_policy], definitions)

    # Render the the ASA configuration from the configs object
    acl = configs["cisco_asa_policy.asa"]

    # Remove additional blank lines from rendered ACLs
    acl = re.sub("\n\n\n", "\n", acl)

    return(acl)

if __name__ == "__main__":
    # Print the ACL
    print(makeAsaAcl())  # no qa