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

Refactor loader.py #8560

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Refactor loader.py #8560

wants to merge 9 commits into from

Conversation

Julian-O
Copy link
Contributor

@Julian-O Julian-O commented Jan 12, 2024

This refactor adds no new functionality, but improves the code to improve logging, exception handling and shut-down behaviour.

Each commit tackles a different issue:

  • Unexpected exceptions in worker thread tasks were printed to the screen rather than properly logged. This was a contributing factor to "bad transparency mask" error animated gifs #8555 being hard to debug. (This only addresses Point 3 in my comment there, and does not close the issue.)

  • Avoid raising direct instances of the Exception class - used ValueError instead.

  • Avoid catch blocks which don't specify exceptions. Caught IndexError instead.

  • threading.Condition supports context managers, which simplifies code (ensures acquires and releases are perfectly matched)

  • Code purported to catch an exception if a filename was empty, but didn't. Added an explicit check and logged that fact if it occurred.

  • Ensured files were closed, and file handlers were not reused afterwards.

  • Declared an existing static method as static.

  • Old code checked 4 times a second if it had been stopped. Replaced with a signal to remove the busy wait and wasted CPU.

  • Old code cleaned up a clock action only if the garbage collector happened to run. Now, it is cleaned up on stop().

There are no API changes here. In theory, the existing unit-tests should be sufficient. However, I currently can't see the coverage (most of the unit tests don't run on my Windows dev machine). Waiting for merge to see what is needed for the coveralls report.

Maintainer merge checklist

  • Title is descriptive/clear for inclusion in release notes.
  • Applied a Component: xxx label.
  • Applied the api-deprecation or api-break label.
  • Applied the release-highlight label to be highlighted in release notes.
  • Added to the milestone version it was merged into.
  • Unittests are included in PR.
  • Properly documented, including versionadded, versionchanged as needed.

@Julian-O Julian-O marked this pull request as draft January 12, 2024 16:43
Raise ValueError ("Raised when an operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError."), so they can be specifically caught by clients.
Catch the expected exceptions only. Do not suppress Out Of Memory errors, abort commands and other unexpected exceptions.
Simplify code by using contextmanager.
Old code would catch all exceptions, not just expected exceptions.
In fact, old code had no expected exceptions - code would still run if filename was missing!
Added explicit check to catch missing filename. Logged, because this is probable not a good thing.
Minimise scope of try block.
Consistently set file handles to None after closing.
Simplify truthiness check for _out_filename
The cache belongs to the module, not the instance.
Also a couple of comment edits.
Save CPU (and be more responsive) by not checking for stoppages and grabbing and releasing locks at 4Hz.
The clean-up formerly in __del__ () is now in stop(), where it belongs.

[There is no guarantee that __del__() is called in a timely manner, or even at all, so there should be no non-trivial logic in that method.
Common trap for C++ programmers.]
@Julian-O Julian-O changed the title [Draft] Handle async exceptions Refactor loader.py Jan 13, 2024
@Julian-O Julian-O requested a review from misl6 January 13, 2024 14:59
@Julian-O Julian-O marked this pull request as ready for review January 13, 2024 14:59
@Julian-O Julian-O removed the request for review from misl6 January 20, 2024 02:13
@Julian-O
Copy link
Contributor Author

Please hold off on this review/merge. I am considering a larger refactor that will render this moot.

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

1 participant