Skip to content

fix: reclaim browser, PDF, and institutional resources - #32

Open
jung233 wants to merge 17 commits into
Rimagination:masterfrom
jung233:codex/browser-memory-lifecycle
Open

fix: reclaim browser, PDF, and institutional resources#32
jung233 wants to merge 17 commits into
Rimagination:masterfrom
jung233:codex/browser-memory-lifecycle

Conversation

@jung233

@jung233 jung233 commented Jul 17, 2026

Copy link
Copy Markdown

Summary

  • preserve Playwright and CloakBrowser owner-thread cleanup and release browser permits only after confirmed shutdown
  • close login, cookie capture, institutional authentication, publisher batch, and proxy-rotation browser handles deterministically
  • close PyMuPDF documents and publish validated PDF files atomically
  • install the required Chromium runtime, reap child processes, and bound allocator arenas

Verification

Validation used this exact head in remote containers; the project was not run locally.

  • rebased onto upstream v1.9.0 at 5e4a6f2; GitHub reports mergeable=true
  • Python 3.11 and Python 3.12 source compilation: passed
  • browser, PDF, authentication, publisher, CLI, and MCP imports: passed
  • proxy rotation probe: every configured slot participated and every context was used and closed on its owner thread
  • four real CloakBrowser cycles: 65.3 -> 65.5 -> 65.5 -> 65.6 MiB RSS
  • every cycle reported shutdown=True, reclaim=True, and zero live child processes

Scope

This is the foundational resource-lifecycle PR. It contains 13 focused commits and changes no files under tests/.

@jung233
jung233 force-pushed the codex/browser-memory-lifecycle branch from 1328eff to 7a136ed Compare July 17, 2026 06:50
@jung233 jung233 changed the title fix(browser): reclaim Playwright resources after each lifecycle fix: reclaim browser, PDF, and institutional resources Jul 17, 2026
@jung233
jung233 force-pushed the codex/browser-memory-lifecycle branch from 7a136ed to 987d3de Compare July 17, 2026 08:38
@jung233
jung233 marked this pull request as ready for review July 17, 2026 09:30
Copilot AI review requested due to automatic review settings July 17, 2026 09:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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
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.

2 participants