Skip to content

run shiny help examples in the console instead of locking up - #17908

Merged
kevinushey merged 6 commits into
mainfrom
bugfix/17178-run-examples-shiny-lockup
Jun 9, 2026
Merged

kevinushey merged 6 commits into
mainfrom
bugfix/17178-run-examples-shiny-lockup

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

Problem

Clicking "Run examples" on a help page whose examples launch a blocking
application (for example a Shiny app via shiny::runApp()) locks up RStudio,
recoverable only by Force Quit (#17178).

R's enhanced HTML help renders "Run examples" inline by knitting the example
code on the R event loop while serving the help HTTP request. A blocking
runApp() there never returns, so the help response never arrives (blank
window) and the R REPL stays parked (unresponsive console). Running
example("testfn") at the prompt works fine because it executes at the REPL
top level, where a blocking app is interruptible and expected.

Fix

Intercept the /library/<pkg>/Example/<topic> (and Demo) request in the help
server. If the example's source statically looks like it launches a blocking
application, run it in the console via example() / demo() -- matching the
behavior users get at the prompt -- and return a lightweight page instead of
blocking the help server. Normal examples are untouched and still render
inline.

Detection parses the example/demo source and matches SYMBOL_FUNCTION_CALL
tokens against a conservative launcher list (runApp, runExample,
runGadget, shinyApp, run_tutorial, ...), so launcher names appearing only
in comments or strings do not trigger a false positive.

Known limitation

The launcher list is intentionally conservative (Shiny-family plus
run_tutorial). Other ways an example can block the help server (readline(),
plumber::pr_run(), httpuv::runServer(), ...) are not detected and would
still wedge the session as before; an undetected case is no worse than current
behavior. The list can grow as such cases are reported.

Testing

  • R unit tests for the detector in test-help.R (--scope r --filter help).
  • A Playwright e2e test that installs a throwaway package whose example
    statically references shiny::runApp (guarded by if (FALSE) so it does not
    actually launch), clicks the real "Run examples" link, and asserts both the
    console-routing page and that example() ran in the console.

Fixes #17178.

clicking "run examples" on a help page whose examples launch a blocking
app (e.g. shiny::runApp) rendered them inline via the help server, which
runs the example on the r event loop and never returns, locking up the
session (#17178). detect such examples by parsing the example/demo source
and route them to the console via example()/demo() instead, matching the
behavior of running example() at the prompt. normal examples are
unaffected and still render inline.

addresses #17178
@kevinushey kevinushey added this to the Blue Plumbago milestone Jun 9, 2026
@kevinushey
kevinushey requested a review from gtritchie June 9, 2026 17:58
- make the e2e marker assertion output-only: the Rd builds the marker
  from two halves with cat(sep = ""), so the echoed example source can
  no longer satisfy the assertion
- split command construction into .rs.helpExampleDivertCommand and add
  unit tests covering the demo branch (blocking, benign, and missing
  demos) via a fake package in a temporary library
- suppress warnings when fetching example/demo source
- mirror tools:::httpd()'s separate Example/Demo URL patterns and use
  regex_match instead of an anchored regex_search
- add a back link to the page returned when an example is diverted to
  the console
@github-actions

github-actions Bot commented Jun 9, 2026 •

Copy link
Copy Markdown

🍎 macOS Desktop E2E · ARM64

✅ All tests passed!

████████████████████ 100% pass rate

✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
327 0 11 3
📥 Download Playwright report

⬇️ playwright-report.zip — extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


2 shards · macos-26 ARM64 · commit 424eca4b5d8bed97d24d09bbc61c3ec477717e4d · Run #172

@gtritchie

Copy link
Copy Markdown
Member

PR Review Summary

Reviewed across four aspects: general code quality, test coverage, comment accuracy, and error handling. No critical or merge-blocking issues. The design is sound — every fallback lands on the pre-existing inline path, so the change strictly removes lockups and never makes behavior worse.

Critical Issues (0)

None.

Important Issues (2)

1. helpExampleDivertCommand() "Example" branch is untested — src/cpp/tests/testthat/test-help.R
The unit tests cover only the Demo branch (test-help.R:67). The example(..., give.lines = TRUE) branch (SessionHelp.R:1115) is the primary code path for this fix (#17178 is about "Run examples"), yet has no R unit test — it's only exercised via the single e2e happy path. Mirror the Demo test with an .Rd \examples{} fixture asserting blocking→command and benign→NULL.

2. Back-URL comment is inaccurate, and the link can be dead — src/cpp/session/modules/SessionHelp.cpp:1005-1010
The comment states the example topic "is the Rd base name," so the back-link is ../html/<topic>.html. Verified against tools:::Rd2HTML: the URL topic is actually the Rd's first \alias, not the file basename. These normally coincide (and always for the PR's own fixture), but an Rd whose first alias differs from its filename yields a dead "Back to help topic" link. The example still runs correctly — impact is limited to a possibly-broken back-link. Soften the comment to reflect the common-case assumption, or note the link is best-effort.

Suggestions (5)

  • Detection vs. \dontrun{} mismatch — SessionHelp.R: detection reads source via give.lines = TRUE (which includes \dontrun{}/\donttest{} blocks), but the diverted console example() runs with default run.dontrun = FALSE. A launcher that lives only inside \dontrun{} can therefore trip detection, show the "running in console" page, then not actually launch anything. Harmless (no lockup), but potentially confusing. Worth a comment acknowledging it, at minimum.

  • Add R-side logging before swallowing errors — SessionHelp.R:1109-1122: the blanket tryCatch(error = function(e) NULL) is acceptable graceful degradation, but it erases the only signal that detection mis-fired (e.g. corrupt Rd DB / unreadable demo). Log conditionMessage(e) before returning NULL so a residual lockup stays debuggable.

  • Give the C++ LOG_ERROR context — SessionHelp.cpp:1000: the catch/fallthrough is correct, but LOG_ERROR(error) records an anonymous failure. Per the project's own error-handling standard, include type/package/topic.

  • Cover the full launcher list and deparse quoting — test-help.R:45: only 4 of the 10 launchers are tested (runExample, runUrl, runGist, runGitHub, shinyAppDir, shinyAppFile are untested). A table-driven loop is cheap insurance against a typo in the vector. Also add a test with an unusual topic name to prove deparse() quoting (used because the string is execute = TRUE'd) actually protects the console call.

  • Tighten CLAUDE.md --filter wording — .claude/CLAUDE.md: --filter matches the test name after stripping test-/.R (i.e. help), not the file "basename" (test-help). The worked example is correct; the description is loosely worded.

Recommended Action

  1. Add the missing Example-branch unit test (Important Clone fail-Permission denied (publickey) #1) — the one omission close to a must-fix.
  2. Fix the back-URL comment (Important Autocompletion of delimiters #2); optionally harden the back-link or note it's best-effort.
  3. Consider the low-cost suggestions: launcher-list loop, deparse quoting test, R-side error logging, C++ log context, and the \dontrun{} note.

Nothing here blocks merge. The fix is correct, conservative, and well-tested for its core detector; the gaps are in secondary coverage and debuggability rather than behavior.

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM! Some potential additional tests and comment tweaks in the feedback.

- add unit tests for the Example branch of .rs.helpExampleDivertCommand(),
  using a fabricated installed-package help database, including deparse()
  quoting and dontrun coverage
- cover the full launcher list (bare and namespace-qualified) in the
  .rs.exampleCodeLaunchesApp() tests
- log a warning before swallowing errors when example/demo source can't
  be retrieved
- add type/package/topic context to the C++ LOG_ERROR
- correct the back-URL comment (URL topic is the Rd's first alias, so the
  back-link is best-effort)
- document that give.lines = TRUE comments out dontrun blocks, keeping
  detection consistent with the diverted example() call
- tighten the CLAUDE.md --filter wording
@kevinushey

Copy link
Copy Markdown
Collaborator Author

Thanks for the review! Addressed in 54b9b5a:

  • Added unit tests for the Example branch of .rs.helpExampleDivertCommand(), using a fabricated installed-package help database (Rd lazy-load db + aliases.rds), including a deparse() quoting case via an alias containing quotes.
  • Softened/corrected the back-URL comment: the URL topic is the Rd's first \alias, so the back-link is best-effort.
  • The launcher detection test now covers all 10 launchers, bare and namespace-qualified.
  • .rs.helpExampleDivertCommand() now logs a warning with conditionMessage(e) before swallowing retrieval errors, and the C++ LOG_ERROR carries type/package/topic context.
  • Tightened the CLAUDE.md --filter wording as suggested.

One note on the \dontrun{} suggestion: I checked, and the mismatch doesn't actually exist. example(give.lines = TRUE) applies the same commentDontrun = TRUE default as a real example() call, so code inside \dontrun{} comes back commented out (## Not run: / ##D) and never trips the parse-based detector. Detection and the diverted console call are therefore consistent: a launcher living only inside \dontrun{} is neither detected nor diverted. I documented this in a comment and added a unit test proving a \dontrun{}-only launcher is not diverted.

@kevinushey
kevinushey merged commit c0b19d0 into main Jun 9, 2026
1 check passed
@kevinushey
kevinushey deleted the bugfix/17178-run-examples-shiny-lockup branch June 9, 2026 23:42
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.

Running Shiny in help page example can cause RStudio to lock up

2 participants