Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows port to be specified as an environment variable #963

Merged
merged 10 commits into from
Jan 13, 2025

Conversation

shikokuchuo
Copy link
Member

@shikokuchuo shikokuchuo commented Nov 3, 2024

Fixes #956 (tagged 'help wanted').

The error returned there is an Rcpp error, as the httpuv function that's ultimately called is expecting an integer input and can't handle the implicit character -> integer conversion. Environment variables are by default retrieved as strings.

I've added the as.integer() conversion as a catch-all, rather than add a conditional branch as:

i) I find it slightly more readable
ii) slightly more defensive, in case the methods to find a random port above it are updated
iii) getRandomPort() returns a double so there would have been an implicit conversion at the Rcpp level anyway

Includes a regression test.

PR task list:

  • Update NEWS
  • Add tests
  • Update documentation with devtools::document()

@CLAassistant
Copy link

CLAassistant commented Nov 3, 2024

CLA assistant check
All committers have signed the CLA.

R/find-port.R Outdated
@@ -44,5 +44,5 @@ findPort <- function(port){
stop("Unable to start a Plumber server. Either the port specified was unavailable or we were unable to find a free port.")
}

port
as.integer(port)
Copy link
Member

Choose a reason for hiding this comment

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

The problem is that this doesn't fully handle validation of user input because all of the code above it assumes that port wasn't provided.

For example, given obviously bad user input both doesn't work AND doesn't error.

options(plumber.port = "blue"); plumber::pr_run(plumber::pr())
#> Running plumber API at http://127.0.0.1:NA
#> Running swagger Docs at http://127.0.0.1:NA/__docs__/

Copy link
Member

Choose a reason for hiding this comment

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

I refactored findPort() so that, in the missing port case we immediately hand over control to findRandomPort() which handles finding a random port.

This leaves the rest of findPort() to handle coercing user input to an integer and checking that it's a valid value.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, Thanks. It seems I only addressed the narrower issue that (correct) ports specified via env var were failing. Didn't expect NA to be accepted as a port, should this not fail somewhere else? What happens if an out of port range integer is specified?

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @shikokuchuo, and yeah somehow NA would flow through because findPort() didn't currently do any validating of the port number. You're right, we should also check the valid port range, that wouldn't fail later either. I'll add that to this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Updated with further validation in cf5086c

@gadenbuie gadenbuie requested a review from schloerke January 13, 2025 16:52
gadenbuie and others added 4 commits January 13, 2025 14:30
* In 1024-49151 (following IANA service port numbers)
* Not an unsafe port (using httpuv's unsafe port list)
@schloerke schloerke merged commit 972a19b into rstudio:main Jan 13, 2025
11 checks passed
schloerke added a commit that referenced this pull request Feb 5, 2025
* main: (28 commits)
  feat: add `excel` serializer/parser (#975)
  ci: Error on spelling (#981)
  docs: Improve forward docs a bit (#978)
  Fix file path tests on windows (#979)
  bug: Update default debug behaviour to `FALSE` (#976)
  perf: Avoid disk I/O during parsing (#972)
  docs: Add note on annotating required parameters (#971)
  Allows port to be specified as an environment variable (#963)
  tests: Replace `with_mock()` with `with_mocked_bindings()` (#970)
  feat(serializer): Add support for ragg and svglite (#964)
  chore: Fix pkgdown warnings. Use lifecycle inline R badges (#965)
  v1.2.2 release candidate (#948)
  bug: Use `{rlang}` instead of `{ellipsis}` (#958)
  docs: Update URLs (#954)
  docs: fix table spacing (#951)
  docs: change link to httr2 in routing and input vignette (#944)
  Specify interactive mode in doc (#932)
  Fall back to sys env var if R option doesn't exist (#934)
  Add support for quoted boundary for multipart request parsing. (#924)
  chore: Remove parse comment to match implementation (#931)
  ...
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.

Plumber crashes when specifying port via environment variable
4 participants