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

Improve file handling with lazy reads #363

Merged
merged 6 commits into from Dec 6, 2023
Merged

Conversation

sirosen
Copy link
Member

@sirosen sirosen commented Dec 5, 2023

Primarily this changeset introduces lazy file reading to resolve #352.
Additionally, it introduces a better caching mechanism to the schema file
readers so that schemas read from ephemeral sources (like pipes) can properly
be applied to multiple instances.

See also: pallets/click#2645

  • Apply lazy file reading for instancefiles
  • Fix schema readers to cache data
  • Add a test which verifies lazy file reading

Lazy reads are slightly customized to better handle named pipes.

The lazy file reads are combined with an early close immediately after
the instance loader gets data from a file. As a result,
check-jsonschema should only keep one instance file open at a time.
The previous implementation tried to repeatedly read data for a schema
input, even across multiple instances. As a result, if the schema
source was ephemeral (e.g. a memfd), the subsequent reads would fail.
Create a directory of 2000 instance files, then iterate through them
and verify that the number of open files never exceeds 2000 at any
point during iteration.
In order for pyjson5 to be satisfied, it must be passed the real file
descriptor (or something satisfying `io.IOBase`). For simplicity, pass
it the underlying open() result.

Also improve the file close behavior using a `finally` block.
@sirosen sirosen merged commit 16a6062 into main Dec 6, 2023
45 checks passed
@sirosen sirosen deleted the improve-file-handling branch December 6, 2023 02:20
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.

Opening concurrent file handles for all instancefiles risks breaching OS limit.
1 participant