Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close files of engine logger #35

Merged
merged 3 commits into from
Jul 29, 2022

Conversation

flandweber
Copy link
Contributor

When running evolve.py the file descriptors for the individual logfiles of all started engine instances are not closed on exit of the instance. The open files can be viewed with ls -l /proc/<pid OR "self">/fd/.

This makes the engine collect hundreds of open files. The default limit is 1024 (ulimit -n) after which evolve.py (python evolve.py --test-type http --port 80 --log debug) crashes with

OSError: [Errno 24] Too many open files
Traceback (most recent call last):
  File "/home/vagrant/geneva/evolve.py", line 835, in <module>
    driver(sys.argv[1:])
  File "/home/vagrant/geneva/evolve.py", line 814, in driver
    hall_of_fame = genetic_solve(logger, options, ga_evaluator)
  File "/home/vagrant/geneva/evolve.py", line 552, in genetic_solve
    offspring = fitness_function(logger, offspring, ga_evaluator)
  File "/home/vagrant/geneva/evolve.py", line 207, in fitness_function
    return ga_evaluator.evaluate(population)
  File "/home/vagrant/geneva/evaluator.py", line 184, in evaluate
    self.worker(ind_list, "main", environment)
  File "/home/vagrant/geneva/evaluator.py", line 808, in worker
    eid, fitness = self.run_test(environment, ind)
  File "/home/vagrant/geneva/evaluator.py", line 239, in run_test
    self.plugin.start(self.args, self, environment, ind, self.logger)
  File "/home/vagrant/geneva/plugins/http/plugin.py", line 143, in start
    fitness = evaluator.run_client(evaluator.client_args, environment, logger)
  File "/home/vagrant/geneva/evaluator.py", line 301, in run_client
    self.run_local_client(args, environment, logger)
  File "/home/vagrant/geneva/evaluator.py", line 481, in run_local_client
    self.exec_cmd(command)
  File "/home/vagrant/geneva/evaluator.py", line 494, in exec_cmd
    subprocess.check_call(command, timeout=60)
  File "/usr/lib/python3.9/subprocess.py", line 368, in check_call
    retcode = call(*popenargs, **kwargs)
  File "/usr/lib/python3.9/subprocess.py", line 349, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/usr/lib/python3.9/subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.9/subprocess.py", line 1722, in _execute_child
    errpipe_read, errpipe_write = os.pipe()
OSError: [Errno 24] Too many open files

This PR fixes this by employing the context manager in the main method and closing the logging handler upon exit.

-> fixes problem with running out of file descriptors (usually after 1024 runs)
while True:
time.sleep(0.5)
finally:
eng.shutdown_nfqueue()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we might still want the finally to handle ^C or other catchable exceptions when the engine is invoked directly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The __exit__-method of Engine is always called - even in case of an exception: https://book.pythontips.com/en/latest/context_managers.html#handling-exceptions

Since shutdown_nfqueue is called from there, we can (and should) remove the finally-block in my opinion.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you're right - I was remembering that this code did not use the context manager, and had missed that you swapped it to using it. Thanks for doing it - I'll merge it!

@Kkevsterrr
Copy link
Owner

Hey this is great - thanks for contributing! I thought I fixed this bug a while ago, but I guess not. I left one quick comment in there for you; happy to merge once we address that. Thanks!

@Kkevsterrr Kkevsterrr merged commit 3c1c963 into Kkevsterrr:master Jul 29, 2022
@flandweber flandweber deleted the close-filedescriptors branch July 29, 2022 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants