fix: reclaim browser, PDF, and institutional resources - #32
Open
jung233 wants to merge 17 commits into
Open
Conversation
jung233
force-pushed
the
codex/browser-memory-lifecycle
branch
from
July 17, 2026 06:50
1328eff to
7a136ed
Compare
This was referenced Jul 17, 2026
These functions were referenced in sources/__init__.py but never defined in pdf_utils.py, causing ImportError on every download attempt. - is_suspicious_pdf: checks if PDF is < 50KB (likely preview/cover) - suspicious_pdf: returns failure result and cleans up the file Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
jung233
force-pushed
the
codex/browser-memory-lifecycle
branch
from
July 17, 2026 08:38
7a136ed to
987d3de
Compare
jung233
marked this pull request as ready for review
July 17, 2026 09:30
There was a problem hiding this comment.
Pull request overview
This PR focuses on tightening resource lifecycle management across the downloader stack (Playwright/CloakBrowser handles, HTTP sessions/responses, and PDF file writes), while improving deterministic cleanup and adding runtime dependencies needed for browser/PDF features in containerized deployments.
Changes:
- Introduces cancellation-aware waiting, deterministic closing of browser/context/page resources, and safer teardown paths across institutional sources.
- Adds atomic PDF publishing helpers and uses them across download paths to avoid partial writes and reduce leaked temp files.
- Refactors publisher batch concurrency to avoid sharing Playwright sync contexts across threads; adds worker profile isolation and improved broker/session cleanup.
Reviewed changes
Copilot reviewed 21 out of 21 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/scansci_pdf/sources/instsci.py | Adds cancellation support, session/response cleanup, atomic PDF writes, and deterministic browser resource teardown for institutional WebVPN flows. |
| src/scansci_pdf/sources/ezproxy.py | Adds cancellation-aware waits, atomic PDF writes, and deterministic cleanup for EZProxy browser workflows. |
| src/scansci_pdf/sources/carsi.py | Adds cancellation plumbing, lock acquisition that can be cancelled, atomic PDF writes, and improved session cleanup for CARSI workflows. |
| src/scansci_pdf/sources/carsi_source.py | Threads cancellation into the CARSI standalone source and ensures the CARSI client closes deterministically. |
| src/scansci_pdf/server.py | Makes Elsevier API setup probe more robust (route attempts + cleanup) and ensures CARSI client cleanup in MCP tools. |
| src/scansci_pdf/publisher_batch.py | Refactors parallel execution to avoid cross-thread Playwright context sharing; adds worker profile handling and context invalidation recovery. |
| src/scansci_pdf/pdf_utils.py | Adds atomic PDF write/publish helpers, response↔session binding helper, and cancellation parameterization in download_pdf(). |
| src/scansci_pdf/main.py | Aligns CLI entrypoints with new fetcher/config paths and improves deterministic cleanup in CLI commands. |
| src/scansci_pdf/fetcher.py | Improves response cleanup, listener cleanup, and deterministic auth shutdown in the core fetcher flows. |
| src/scansci_pdf/extractors/pdf_extractor.py | Makes PyMuPDF optional at import time and ensures documents close deterministically after extraction. |
| src/scansci_pdf/deps.py | Declares PyMuPDF as a core dependency for extraction features. |
| src/scansci_pdf/cloakbrowser_compat.py | Adds guarded launch wrapper to stop Playwright driver on CloakBrowser launch failures. |
| src/scansci_pdf/cli.py | Improves broker loop robustness and context invalidation handling; ensures context cleanup at shutdown. |
| src/scansci_pdf/cache.py | Makes cache writes collision-resistant using per-write UUID temp files. |
| src/scansci_pdf/browser_login.py | Adds owner-thread enforcement + deterministic cleanup semantics for persistent browser lifecycle. |
| src/scansci_pdf/browser_engine.py | Adds global browser slot limiting/retention, cancellation-aware waits, robust shutdown confirmation, and safer cookie/tab handling. |
| src/scansci_pdf/browser_cookies.py | Ensures browser/context/page and slot leases close deterministically after cookie extraction. |
| src/scansci_pdf/auth.py | Uses leased browser contexts, strengthens close semantics, and improves response/session cleanup. |
| pyproject.toml | Adds PyMuPDF dependency and ensures cloakbrowser extra is installed in container builds. |
| Dockerfile | Installs Chromium runtime deps + tini, configures cloakbrowser cache dir and allocator arena cap for lower RSS growth. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+366
to
+372
| resp = fetch( | ||
| url, | ||
| config, | ||
| stream=True, | ||
| use_tor=use_tor, | ||
| cancel_event=cancel_event, | ||
| ) |
Comment on lines
106
to
+110
| "error_type": "suspicious_pdf", | ||
| "reason": "PDF appears to be a cover page or preview (too small / too few pages)", | ||
| } | ||
| def _unique_part_path(output_path: Path) -> Path: | ||
| """Return a collision-resistant temporary path beside ``output_path``.""" |
Comment on lines
+856
to
860
| prepare_cloakbrowser_runtime() | ||
| from cloakbrowser import launch | ||
| except ImportError: | ||
| except Exception: | ||
| log.info(" [WebVPN-Browser] cloakbrowser not installed") | ||
| return None |
Comment on lines
183
to
194
| try: | ||
| from ..cloakbrowser_compat import ( | ||
| launch_with_driver_cleanup, | ||
| prepare_cloakbrowser_runtime, | ||
| ) | ||
|
|
||
| prepare_cloakbrowser_runtime() | ||
| from cloakbrowser import launch | ||
| from ..browser_engine import _build_browser_args | ||
| except ImportError: | ||
| except Exception: | ||
| log.info(" [EZProxy] cloakbrowser not installed") | ||
| return None |
Comment on lines
194
to
201
| try: | ||
| from ..cloakbrowser_compat import prepare_cloakbrowser_runtime | ||
|
|
||
| prepare_cloakbrowser_runtime() | ||
| from cloakbrowser import launch # noqa: F401 | ||
| except ImportError: | ||
| except Exception: | ||
| log.info(" [CARSI-Browser] cloakbrowser not installed") | ||
| return None |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Verification
Validation used this exact head in remote containers; the project was not run locally.
Scope
This is the foundational resource-lifecycle PR. It contains 13 focused commits and changes no files under tests/.