Skip to content

[Core] Handle JavaScript injection failures without native callback exceptions - #1854

Merged
r0x0r merged 2 commits into
r0x0r:masterfrom
FiraSenax:codex/handle-js-injection-failure
Oct 20, 2026
Merged

r0x0r merged 2 commits into
r0x0r:masterfrom
FiraSenax:codex/handle-js-injection-failure

Conversation

@FiraSenax

Copy link
Copy Markdown
Contributor

JavaScript resource failures currently escape inject_pywebview() on the native navigation callback's thread. In a contaminated application build, a stray customize 2.js retained %(text_select)s but received no template parameters, raising KeyError through Cocoa's webView_didFinishNavigation_ and aborting the process. Missing/unreadable resources reach the same unguarded call. Separately, the initial run_js(js_code) is outside the worker's exception handler and can leave loaded waiters blocked.

This change catches resource-loading failures at the shared injection boundary and includes the filename in template-format errors. It also moves the initial script execution inside the existing worker handler. Failure sets loaded, consistently with the existing API-injection failure path, but never sets _pywebviewready. Scripts are not silently skipped and resource loading/discovery rules stay unchanged.

Validation on macOS / Python 3.11:

  • Added unit regressions for loader failures, initial/API injection failures, the successful path, and filename diagnostics. Four cases fail before the fix and pass afterward.
  • Added a native window regression with a malformed JS resource: the Cocoa window can load and close without aborting, while the bridge remains unready.
  • pytest tests/test_util.py tests/test_js_api.py tests/test_load_html.py -q: 45 passed, including the existing bridge and HTML-loading tests.
  • All configured pre-commit hooks passed on the four changed files.

The fix is in the shared injection code; backend modules are unchanged. Native Windows/Linux/Android backends and the full integration suite have not been run locally.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The narrow exception-handling changes preserve successful initialization, with no unresolved findings.

0 open findings

What changed in this PR

Hardens pywebview’s shared JavaScript injection path so resource and execution failures are handled without escaping native callbacks or blocking load waiters.

Changes:

  • Catches resource-loading and initial injection errors; template errors identify the failing file.
  • Adds unit and native-window regressions for failure handling and bridge readiness.
  • Documents the behavior in the changelog.
File Description
webview/​util.py Guards injection failures and improves template diagnostics.
tests/​test_util.py Tests failure handling, successful injection, and diagnostics.
tests/​test_js_api.py Adds a malformed-resource native-window regression.
docs/​CHANGELOG.md Documents the shared injection fix.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@r0x0r

r0x0r commented Oct 19, 2026

Copy link
Copy Markdown
Owner

@copilot please fix the merge conflicts in this pull request.

@r0x0r
r0x0r merged commit 9596a65 into r0x0r:master Oct 20, 2026
9 checks passed
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.

3 participants