Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Explains when the latest rsconnect-python release requires a different Python version and suggests installing it with `uv tool install`.

## [1.33.1] - 2026-11-09

### Fixed
Expand Down
123 changes: 69 additions & 54 deletions rsconnect/version_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@

import json
import os
import platform
import sys
import threading
import time
from http.client import HTTPSConnection
from os.path import join
from typing import Optional, Tuple
from typing import NamedTuple, Optional, Tuple

from packaging.specifiers import InvalidSpecifier, SpecifierSet
from packaging.version import Version

from . import VERSION
Expand All @@ -27,10 +30,15 @@
_PYPI_HOST = "pypi.org"
_PYPI_PATH = "/pypi/rsconnect-python/json"
_PYPI_TIMEOUT_SECONDS = 2
_CACHE_TTL_SECONDS = 24 * 60 * 60 # Re-check PyPI at most once a day.
_CACHE_TTL_SECONDS = 24 * 60 * 60
_CACHE_FILENAME = "version_check.json"


class _Release(NamedTuple):
version: str
requires_python: Optional[str]


def _is_check_disabled() -> bool:
value = os.environ.get(RSCONNECT_DISABLE_VERSION_CHECK, "").strip().lower()
return value in ("1", "true", "yes")
Expand All @@ -47,108 +55,115 @@ def _cache_path() -> str:
return join(config_dirname(), _CACHE_FILENAME)


def _read_cache() -> Tuple[bool, Optional[str]]:
"""Return ``(is_fresh, latest)``.

``latest`` is the last known PyPI version recorded in the cache (or None when
the cache is missing/unreadable or the last fetch failed). It is returned
regardless of freshness so even a stale value can drive the upgrade hint while
a refresh runs in the background. ``is_fresh`` is True only when the entry is
within the TTL, signalling that no background refresh is needed.
"""
def _read_cache() -> Tuple[bool, Optional[_Release]]:
"""Return freshness and the cached release, retaining stale data for the hint."""
try:
with open(_cache_path()) as f:
data = json.load(f)
latest = data.get("latest")
latest = latest if isinstance(latest, str) else None
is_fresh = time.time() - float(data["checked_at"]) <= _CACHE_TTL_SECONDS
return (is_fresh, latest)
requires_python = data.get("requires_python")
release = (
_Release(latest, requires_python if isinstance(requires_python, str) else None)
if isinstance(latest, str)
else None
)
is_fresh = "requires_python" in data and time.time() - float(data["checked_at"]) <= _CACHE_TTL_SECONDS
return (is_fresh, release)
except Exception:
return (False, None)


def _write_cache(latest: Optional[str]) -> None:
"""Persist the latest known version (or None) with the current timestamp.

A None value is still cached so repeated failures don't re-hit PyPI on every
deploy until the TTL expires.
"""
def _write_cache(release: Optional[_Release]) -> None:
"""Cache failures too, so they do not trigger another request until the TTL expires."""
try:
path = _cache_path()
makedirs(path)
with open(path, "w") as f:
json.dump({"checked_at": time.time(), "latest": latest}, f)
json.dump(
{
"checked_at": time.time(),
"latest": release.version if release else None,
"requires_python": release.requires_python if release else None,
},
f,
)
except Exception:
pass


def _fetch_latest_version() -> Optional[str]:
def _fetch_latest_version() -> Optional[_Release]:
conn = None
try:
conn = HTTPSConnection(_PYPI_HOST, timeout=_PYPI_TIMEOUT_SECONDS)
conn.request("GET", _PYPI_PATH, headers={"Accept": "application/json"})
response = conn.getresponse()
if response.status != 200:
return None
data = json.loads(response.read())
return data.get("info", {}).get("version")
info = json.loads(response.read()).get("info", {})
latest = info.get("version")
requires_python = info.get("requires_python")
if isinstance(latest, str):
return _Release(latest, requires_python if isinstance(requires_python, str) else None)
return None
except Exception:
return None
finally:
if conn is not None:
conn.close()


def _update_message(latest: Optional[str]) -> Optional[str]:
"""Return an upgrade warning if ``latest`` is newer than the running version."""
def _update_message(release: Optional[_Release]) -> Optional[str]:
"""Return an upgrade warning if the release is newer than the running version."""
try:
if latest is not None and Version(latest) > Version(VERSION):
if release is None or Version(release.version) <= Version(VERSION):
return None
python_version = platform.python_version()
try:
compatible = not release.requires_python or SpecifierSet(release.requires_python).contains(
python_version, prereleases=True
)
except InvalidSpecifier:
compatible = True
if not compatible:
force = "" if os.path.isfile(join(sys.prefix, "uv-receipt.toml")) else "--force "
return (
f"A new version of rsconnect-python is available: {latest} "
f"(you have {VERSION}).\n"
f"Upgrade with: pip install --upgrade rsconnect-python"
f"The latest version of rsconnect-python ({release.version}) requires Python "
f"{release.requires_python}, but you are running Python {python_version}.\n"
f'Install it with: uv tool install {force}--python "{release.requires_python}" rsconnect-python'

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.

I know this is the direction we're going in so its fine, but this instruction being uv specific means that anyone who installed a different way gets a command for a tool they may not have (or they could end up with two installed versions in different locations right?). Again, probably fine, just calling it out here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

anyone who installed a different way gets a command for a tool they may not have

It's entirely possible, but given how well uv tool fits into the problem space we are addressing here, I think its an ok tradeoff for the time being. If it turns out that access to uv ends up being a blocker, then we should investigate other solutions.

)
return (
f"A new version of rsconnect-python is available: {release.version} "
f"(you have {VERSION}).\n"
"Upgrade with: pip install --upgrade rsconnect-python"
)
except Exception:
pass
return None
return None


class BackgroundVersionCheck:
"""Resolves the latest available version, using a cache and a background fetch."""

def __init__(self) -> None:
self._thread: Optional[threading.Thread] = None
self._latest: Optional[str] = None
self._release: Optional[_Release] = None

def start(self) -> None:
if _is_check_disabled() or _is_dev_version(VERSION):
return
is_fresh, latest = _read_cache()
# Seed the hint from the cached value (even a stale one is good enough to
# suggest an upgrade), so a warm cache prints instantly on every exit path
# -- including commands that fail fast.
self._latest = latest
is_fresh, self._release = _read_cache()
if not is_fresh:
# Refresh in the background so the fetch overlaps the command's own
# work. A warm or stale cache never waits on this; only a cold cache
# waits briefly at exit -- see get_warning_message.
self._thread = threading.Thread(target=self._run, daemon=True)
self._thread.start()

def _run(self) -> None:
latest = _fetch_latest_version()
_write_cache(latest)
# Adopt a successful result so a cold-cache run can warn this time, but
# never overwrite a usable cached value with a failed (None) refresh.
if latest is not None:
self._latest = latest
release = _fetch_latest_version()
_write_cache(release)
# Keep a usable stale value when a refresh fails.
if release is not None:
self._release = release

def get_warning_message(self) -> Optional[str]:
# With no cached value yet (a cold cache: first run, or an ephemeral
# environment where the cache never persists), give the in-flight fetch a
# bounded chance to finish so this run can still warn. The bound is the
# fetch's own time budget -- a slower network just means no hint this run.
# A warm or stale cache already has a value here and never waits.
if self._latest is None and self._thread is not None:
# Only a cold cache waits briefly for the background fetch.
if self._release is None and self._thread is not None:
self._thread.join(timeout=_PYPI_TIMEOUT_SECONDS)
return _update_message(self._latest)
return _update_message(self._release)
Loading
Loading