Repository navigation
TIMX 739 - lxml memory pointer bug #286
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import threading | ||
| import itertools | ||
| from concurrent.futures import ThreadPoolExecutor | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import smart_open # type: ignore[import-untyped] | ||
|
|
@@ -12,6 +13,8 @@ | |
| if TYPE_CHECKING: | ||
| from collections.abc import Iterator | ||
|
|
||
| READ_CHUNK_SIZE = 100 | ||
|
|
||
|
|
||
| class XMLTransformer(Transformer): | ||
| """XML transformer class.""" | ||
|
|
@@ -21,9 +24,25 @@ def read_source_file(cls, source_file: str) -> Iterator[bytes]: | |
| """ | ||
| Read XML file and yield each <record> element as bytes. | ||
|
|
||
| The file is read on a single, dedicated thread. pyarrow may resume this generator | ||
| under a new thread state each time, and lxml has a string dictionary per thread | ||
| state. If iterparse finishes under a different thread state than it started, one | ||
| that has parsed something else, lxml replaces the document's dictionary and the | ||
| application aborts with a memory fault when that document is freed. | ||
|
|
||
| Args: | ||
| source_file: A file containing source records to be transformed. | ||
| """ | ||
| records = cls._iterparse_source_file(source_file) | ||
| with ThreadPoolExecutor(max_workers=1) as executor: | ||
| while chunk := executor.submit( | ||
| lambda: list(itertools.islice(records, READ_CHUNK_SIZE)) | ||
| ).result(): | ||
| yield from chunk | ||
|
|
||
| @classmethod | ||
| def _iterparse_source_file(cls, source_file: str) -> Iterator[bytes]: | ||
| """Iteratively parse an XML file and yield each <record> element as bytes.""" | ||
| with smart_open.open(source_file, "rb") as file: | ||
| for _, element in etree.iterparse( | ||
| file, | ||
|
|
@@ -42,18 +61,7 @@ def parse_source_record(cls, source_record: bytes) -> Tag: | |
| Args: | ||
| source_record: An XML source record as bytes. | ||
| """ | ||
| return cls.parse_bs4_in_isolated_thread(source_record) | ||
|
|
||
| @classmethod | ||
| def parse_bs4_in_isolated_thread(cls, source_record: bytes) -> Tag: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This previous workaround is no longer needed given the more root level fix at |
||
| def parse() -> Tag: | ||
| nonlocal source_record | ||
| source_record = BeautifulSoup(source_record, "xml") | ||
|
|
||
| thread = threading.Thread(target=parse) | ||
| thread.start() | ||
| thread.join() | ||
| return source_record | ||
| return BeautifulSoup(source_record, "xml") | ||
|
|
||
| @classmethod | ||
| def get_main_titles(cls, _source_record: Tag) -> list[Tag]: | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is ultimately the root level fix.
First, we assigns
recordsas the lazy iterator coming fromlxml.iterparse().Then, under a single, throwaway thread, we yield chunks from this iterator.
It's eventually cleanly cleaned up because the entirety of the
iterparse()calls completes in a single thread; the explicit one we create here.