Skip to content

fix: ensure PdfReader is closed#8636

Merged
ogabrielluiz merged 1 commit into
langflow-ai:mainfrom
roudy16:main
Jun 20, 2025
Merged

fix: ensure PdfReader is closed#8636
ogabrielluiz merged 1 commit into
langflow-ai:mainfrom
roudy16:main

Conversation

@roudy16
Copy link
Copy Markdown
Contributor

@roudy16 roudy16 commented Jun 19, 2025

The issue is that objects created by the PdfReader have a tendency to linger in memory.

I was memory profiling with tracemalloc my use-case and noticed a lot of Pdf-related objects in the summary. After applying this change the objects didn't show up in the trace summary and my memory consumption post-run was reduced.

Summary by CodeRabbit

  • Refactor
    • Improved resource management when processing PDF files to ensure files are properly closed after use. No changes to user-facing features.

@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jun 19, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 19, 2025

Walkthrough

The parse_pdf_to_text function was updated to use a single with statement that manages both the file opening and the PdfReader instantiation as context managers. This change improves resource management by ensuring both are properly closed after use. No changes were made to the function's signature.

Changes

File(s) Change Summary
src/backend/base/langflow/base/data/utils.py Refactored parse_pdf_to_text to use a single with statement for file and PdfReader context management.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Utils (parse_pdf_to_text)
    participant File
    participant PdfReader

    Caller->>Utils (parse_pdf_to_text): Call with file_path
    Utils (parse_pdf_to_text)->>File: Open file as context manager
    Utils (parse_pdf_to_text)->>PdfReader: Instantiate as context manager with file
    PdfReader-->>Utils (parse_pdf_to_text): Extract text
    Utils (parse_pdf_to_text)->>File: Close file (automatically)
    Utils (parse_pdf_to_text)->>PdfReader: Close reader (automatically)
    Utils (parse_pdf_to_text)-->>Caller: Return extracted text
Loading

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cf985bd and c99f8a1.

📒 Files selected for processing (1)
  • src/backend/base/langflow/base/data/utils.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Update Starter Projects
  • GitHub Check: Run Ruff Check and Format
  • GitHub Check: Ruff Style Check (3.13)
🔇 Additional comments (1)
src/backend/base/langflow/base/data/utils.py (1)

133-134: ```shell
#!/bin/bash
set -e

Install pypdf to test PdfReader context manager support

pip install pypdf

Test PdfReader for context manager methods

python3 - << 'EOF'
from pypdf import PdfReader

has_enter = hasattr(PdfReader, 'enter')
has_exit = hasattr(PdfReader, 'exit')

print(f'PdfReader has enter: {has_enter}')
print(f'PdfReader has exit: {has_exit}')
print(f'Context manager support: {has_enter and has_exit}')
print(f'MRO: {[cls.name for cls in PdfReader.mro]}')
EOF


</details>

</blockquote></details>

</details>
<!-- internal state start -->


<!-- DwQgtGAEAqAWCWBnSTIEMB26CuAXA9mAOYCmGJATmriQCaQDG+Ats2bgFyQAOFk+AIwBWJBrngA3EsgEBPRvlqU0AgfFwA6NPEgQAfACgjoCEYDEZyAAUASpETZWaCrKNwSPbABsvkCiQBHbGlcSHFcLzpIACIAM3gADy4yB39rWlibEjQlPlQGL3xEOgAaaPtsAWZ1Gno5SGxivgp8bFpZAEYANhL0Wlp/RGLkNEg2ZnwXMcw0UjYMUKQHD38vaiiCMNgPKwysnMoFZm58cgWNGG2eFoFI5hQlBfh4qNRcWGp+YVFcZAZ/dZ1eS7TLZXJhMj0Tb+ZjaLDwLDjSbyQoYUh8d6YSDkBjSRDOWS9SI5BFEML4FAYf7ZYr0JFTJgYBzHcSnC7uBpNSC0JBMKT+KEIZBLYKQADuCEiPEosUm1TRYxIEymvHw8S8pM5mveHgABrgqLjYT58AxdeT8F5ehL4AxYPZYPgxVFRoh4EQMM9bZhQrxpGRcfxYtYACIAMTAq0BXxEYmFWB1iuV8mZsJc7KuqpOtMg8QSkBS2EGW0+iZB+3BqCzlC88gKRRdsRofEaJGtCDt1xIUgWyB1xRjP2QsRa9w1aMomoRSeRFwAgiM/NJvLheon6XXTszuKysGgm4cKNgMJ6FYnVbihihkALsLjaL1MIKduHIyQ1rVB3HsRTUej0Nw3DZHkCZXBuYSGh4qYEhmHjcN4vj+EEIToPAzB9hSaGqlIS6IK0FCBrCGCzEq7DoBg9C3oRSrIpyJGUuKkwANaxIUYrxhIloSJq5ZgpQGhGPoxjgFAkJBjgBDEGQyifkwrDsFwvBfuIUgyJuuQqGomjaLoYCGCYUBwKgqBYmgeCEKQ5BULJLDzJwfhoGKFROFM9RMBpqjqFoOhCcJpgGIgBEAPQCGgDBMZCIVoMUQVrGirFOlFMW0NQaBBXg8BeIgGjcLIHAGNEhUGBYkBzgAklJVnRtBUxqowHwTogbhXLq3DOMUAD63AZB1BC9SQCS4OasTHmI8CnOK0VjIoXobBS+BAaBL5hrmmUeE+lKILgPrwOsWx6rxBwUOagixqEYrqAge72KSUq6hd7zmlt6x2VqCqjIyNCDdMxH/rKfACPg7z7atUobYmAK5BcVj+NxrSILWa5XOqHhilNC3SfQ06jPdl3mrcppMY+FEg7qh25OaaPxs9TzVSQbXWe+8gPY6eDoAoCwDaEREkRQFwAHJOr0gPA4mKPkc+kBk3sfHHegaQ86QUL4KQOogfw5BSyzT3bTQdm9IWFCatWzTSPhhEzHMZEbWZBCwuIDCMIUboKnVIv2mLa0SyDkP8eYlhzl4zbUONTLkiDSgFM4IdbuJA0nBQn6TJ4ty2gWTziNIgmQIL9WYKQGEg26HrUEWHh1a17UkF1PV9V9uAABQo111CwFwW0UAAlLoej2Aa5rTrqgUMFF4WRaFMVxUQCViklJBBSl23peIWU5bIuoFUVBi+QZ6f0HVtsWdJjP0HJdlcFQTk1fIbmKMonnaT5ekGLvZ/qB18C0IgHWw/AJDOrQDqz1E66UMC/ESjAACckDYgAA40AdA6AIDouIADMAB2BgDAugACYAAMeDIE4K6AAFjwbEHIqC8GwMgQAVlgXg9BqDYECBIaAvyUA364A/l/H+3Y/4AI6mJfQQA -->

<!-- internal state end -->
<!-- finishing_touch_checkbox_start -->

<details open="true">
<summary>✨ Finishing Touches</summary>

- [ ] <!-- {"checkboxId": "7962f53c-55bc-4827-bfbf-6a18da830691"} --> 📝 Generate Docstrings
<details open="true">
<summary>🧪 Generate Unit Tests</summary>

- [ ] <!-- {"checkboxId": "f47ac10b-58cc-4372-a567-0e02b2c3d479", "radioGroupId": "utg-output-choice-group-unknown_comment_id"} -->   Create PR with Unit Tests
- [ ] <!-- {"checkboxId": "07f1e7d6-8a8e-4e23-9900-8731c2c87f58", "radioGroupId": "utg-output-choice-group-unknown_comment_id"} -->   Post Copyable Unit Tests in Comment

</details>

</details>

<!-- finishing_touch_checkbox_end -->
<!-- tips_start -->

---

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

<details>
<summary>❤️ Share</summary>

- [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai)
- [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai)
- [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai)
- [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)

</details>

<details>
<summary>🪧 Tips</summary>

### Chat

There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=langflow-ai/langflow&utm_content=8636):

- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
  - `I pushed a fix in commit <commit_id>, please review it.`
  - `Explain this complex logic.`
  - `Open a follow-up GitHub issue for this discussion.`
- Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples:
  - `@coderabbitai explain this code block.`
  -	`@coderabbitai modularize this function.`
- PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
  - `@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.`
  - `@coderabbitai read src/utils.ts and explain its main purpose.`
  - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.`
  - `@coderabbitai help me debug CodeRabbit configuration file.`

### Support

Need help? Create a ticket on our [support page](https://www.coderabbit.ai/contact-us/support) for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

### CodeRabbit Commands (Invoked using PR comments)

- `@coderabbitai pause` to pause the reviews on a PR.
- `@coderabbitai resume` to resume the paused reviews.
- `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
- `@coderabbitai full review` to do a full review from scratch and review all the files again.
- `@coderabbitai summary` to regenerate the summary of the PR.
- `@coderabbitai generate docstrings` to [generate docstrings](https://docs.coderabbit.ai/finishing-touches/docstrings) for this PR.
- `@coderabbitai generate sequence diagram` to generate a sequence diagram of the changes in this PR.
- `@coderabbitai auto-generate unit tests` to generate unit tests for this PR.
- `@coderabbitai resolve` resolve all the CodeRabbit review comments.
- `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository.
- `@coderabbitai help` to get help.

### Other keywords and placeholders

- Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed.
- Add `@coderabbitai summary` to generate the high-level summary at a specific location in the PR description.
- Add `@coderabbitai` anywhere in the PR title to generate the title automatically.

### Documentation and Community

- Visit our [Documentation](https://docs.coderabbit.ai) for detailed information on how to use CodeRabbit.
- Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback.
- Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.

</details>

<!-- tips_end -->

@dosubot dosubot Bot added the bug Something isn't working label Jun 19, 2025
@github-actions github-actions Bot added bug Something isn't working and removed bug Something isn't working labels Jun 19, 2025
Copy link
Copy Markdown
Contributor

@ogabrielluiz ogabrielluiz left a comment

Choose a reason for hiding this comment

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

Thank you @roudy16 ! Good catch

This LGTM

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jun 20, 2025
@ogabrielluiz ogabrielluiz enabled auto-merge June 20, 2025 14:59
@ogabrielluiz ogabrielluiz added this pull request to the merge queue Jun 20, 2025
Merged via the queue into langflow-ai:main with commit 5d0abf4 Jun 20, 2025
42 of 44 checks passed
ogabrielluiz pushed a commit to bkatya2001/langflow that referenced this pull request Jun 24, 2025
Khurdhula-Harshavardhan pushed a commit to JigsawStack/langflow that referenced this pull request Jul 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants