Skip to content

Studio: browse temporary Linux mounts under /media and /mnt - #10048

Merged
danielhanchen merged 10 commits into
unslothai:mainfrom
Souravrajvi0:feat/linux-temp-mounts-4402
Oct 1, 2026
Merged

danielhanchen merged 10 commits into
unslothai:mainfrom
Souravrajvi0:feat/linux-temp-mounts-4402

Conversation

@Souravrajvi0

@Souravrajvi0 Souravrajvi0 commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #9818

The model folder picker only allowlisted udisks volumes at /run/media/<user>/<volume>. On Ubuntu, temporary USB and external drives usually appear under /media/<user>/<volume>, /media/<volume>, or /mnt/<name>, so browsing them returned 403 until the user set up an fstab entry.

This change:

  • Discovers readable volumes under /media (user-scoped and legacy layouts) and named mounts under /mnt
  • Feeds them into the same folder-browser allowlist and quick-pick chips as /run/media (managed accounts still browse only their workspace)
  • Dedupes when /media entries symlink into /run/media
  • Skips credential and config dirs (.ssh, .aws, ...)
  • Bounds /mnt probes so a stale network mount cannot stall the picker

Tests

pytest studio/backend/tests/test_linux_external_media_paths.py studio/backend/tests/test_windows_external_drive_paths.py studio/backend/tests/test_browse_denylist.py studio/backend/hub/tests/test_model_services.py

Before / after

Folder browser opened from Hub > Downloaded > Add folder > Browse locations, on a Linux host whose only extra mount is /mnt/disks. Before, /mnt/disks has no quick-pick chip and browsing it returns 403; after, the chip appears and browsing it returns 200.

Folder browser quick-pick chips before and after

The model folder picker only auto-discovered udisks volumes at
/run/media/<user>/<volume>. Ubuntu still automounts at /media and many
users mount extra disks at /mnt without fstab. Include those roots in
the browse allowlist and chips (unslothai#9818).
@Souravrajvi0
Souravrajvi0 force-pushed the feat/linux-temp-mounts-4402 branch from 1dfaf0f to 34c717e Compare August 31, 2026 05:53
Keep mount-root discovery for the folder picker. /media and /mnt are
not on the scan-folder denylist, so the extra predicates were unused.
Document-folder policy only consults this helper when the matched
sensitive root is /run. /media and /mnt browse roots stay in Python.
@unslothai unslothai deleted a comment from Souravrajvi0 Oct 1, 2026
@unslothai unslothai deleted a comment from Souravrajvi0 Oct 1, 2026
@unslothai unslothai deleted a comment from Souravrajvi0 Oct 1, 2026
@unslothai unslothai deleted a comment from Souravrajvi0 Oct 1, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 1, 2026
@shimmyshimmer

Copy link
Copy Markdown
Member

Review record for #10048, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#296.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror 706e95f6d (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] on studio/backend/utils/paths/external_media.py (on the mirror)

@@ -105,26 +140,88 @@ def linux_run_media_mount_roots(
     except (OSError, RuntimeError, ValueError):
         return []
     for volume_dir in volume_dirs:
-        if is_sensitive_path_component(volume_dir.name):
+        accepted = _accept_linux_volume(volume_dir, resolved_base, min_parts = 2, seen = seen)
+        if accepted is not None:
+            roots.append(accepted)
+    return roots
+
+
+def linux_media_mount_roots(
+    base: Path | str = "/media",
+    *,
+    user: str | None = None,
+    run_media: Path | str = "/run/media",
+) -> list[Path]:
+    """Readable ``/media/<user>/<volume>`` and legacy ``/media/<volume>`` roots; entries may symlink into *run_media*."""
+    if platform.system() != "Linux":
+        return []
+    user = user or _current_username()
+    base_path = Path(base)
+    try:
+        resolved_base = base_path.resolve()
+        children = list(base_path.iterdir())
+    except (OSError, RuntimeError, ValueError):
+        return []
+
+    roots: list[Path] = []
+    seen: set[str] = set()
+    for child in children:
+        if is_sensitive_path_component(child.name):
             continue
         try:
-            resolved = volume_dir.resolve()
+            if not child.is_dir():
+                continue
+            # The current user's folder is the parent of their mounts, not a volume.
+            volume_dirs = list(child.iterdir()) if user and child.name == user else [child]
         except (OSError, RuntimeError, ValueError):
             continue
-        if not _is_linux_media_mount_path(str(resolved), resolved_base):
-            continue
-        if _contains_sensitive_media_component(resolved, resolved_base):
-            continue
-        key = os.path.normcase(os.path.realpath(str(resolved)))
-        if key in seen:
-            continue
-        try:
-            is_dir = resolved.is_dir()
-        except OSError:
+        for volume_dir in volume_dirs:
+            accepted = _accept_linux_volume(
+                volume_dir, resolved_base, min_parts = 1, seen = seen, alias_root = run_media
+            )
+            if accepted is not None:
+                roots.append(accepted)
+    return roots
+
+
+def linux_mnt_mount_roots(base: Path | str = "/mnt") -> list[Path]:
+    """Readable ``/mnt/<name>`` roots; probed with a timeout so a stale network mount cannot stall the browser."""
+    if platform.system() != "Linux":
+        return []
+    base_path = Path(base)
+    try:
+        resolved_base = base_path.resolve()
+        children = list(base_path.iterdir())
+    except (OSError, RuntimeError, ValueError):
+        return []
+    readable = _readable_dirs_within(
+        [str(c) for c in children if not is_sensitive_path_component(c.name)],
+        _DRIVE_PROBE_TIMEOUT_S,
+    )
+    roots: list[Path] = []
+    seen: set[str] = set()
+    for child in children:
+        if str(child) not in readable:
             continue
-        if is_dir and os.access(resolved, os.R_OK | os.X_OK):
+        accepted = _accept_linux_volume(child, resolved_base, min_parts = 1, seen = seen)

P2 Badge Preserve system-directory filtering for WSL drive mounts

When Studio runs under WSL, /mnt/c is the standard mapping for the Windows system drive, so accepting every readable /mnt/<name> makes the entire C: drive browseable. The downstream studio_db._denied_path_prefixes() chooses the Linux denylist in this environment and therefore does not reject /mnt/c/Windows or /mnt/c/Program Files, although native Windows explicitly filters those directories. Detect WSL drive roots and apply the translated Windows denylist, or avoid adding the system drive as an unrestricted mount root.

Useful? React with 👍 / 👎.

Accepted on the mirror

Reply from danielhanchen:

Fixed in cf39de3: under WSL, //Windows, Program Files and Program Files (x86) are denied case-insensitively, matching the native Windows denylist.

chatgpt-codex-connector[bot] on studio/backend/utils/paths/external_media.py (on the mirror)

@@ -105,26 +140,88 @@ def linux_run_media_mount_roots(
     except (OSError, RuntimeError, ValueError):
         return []
     for volume_dir in volume_dirs:
-        if is_sensitive_path_component(volume_dir.name):
+        accepted = _accept_linux_volume(volume_dir, resolved_base, min_parts = 2, seen = seen)
+        if accepted is not None:
+            roots.append(accepted)
+    return roots
+
+
+def linux_media_mount_roots(
+    base: Path | str = "/media",
+    *,
+    user: str | None = None,
+    run_media: Path | str = "/run/media",
+) -> list[Path]:
+    """Readable ``/media/<user>/<volume>`` and legacy ``/media/<volume>`` roots; entries may symlink into *run_media*."""
+    if platform.system() != "Linux":
+        return []
+    user = user or _current_username()
+    base_path = Path(base)
+    try:
+        resolved_base = base_path.resolve()
+        children = list(base_path.iterdir())
+    except (OSError, RuntimeError, ValueError):
+        return []
+
+    roots: list[Path] = []
+    seen: set[str] = set()
+    for child in children:
+        if is_sensitive_path_component(child.name):
             continue
         try:
-            resolved = volume_dir.resolve()
+            if not child.is_dir():
+                continue
+            # The current user's folder is the parent of their mounts, not a volume.
+            volume_dirs = list(child.iterdir()) if user and child.name == user else [child]
         except (OSError, RuntimeError, ValueError):
             continue
-        if not _is_linux_media_mount_path(str(resolved), resolved_base):
-            continue
-        if _contains_sensitive_media_component(resolved, resolved_base):
-            continue
-        key = os.path.normcase(os.path.realpath(str(resolved)))
-        if key in seen:
-            continue
-        try:
-            is_dir = resolved.is_dir()
-        except OSError:
+        for volume_dir in volume_dirs:
+            accepted = _accept_linux_volume(
+                volume_dir, resolved_base, min_parts = 1, seen = seen, alias_root = run_media
+            )
+            if accepted is not None:
+                roots.append(accepted)
+    return roots
+
+
+def linux_mnt_mount_roots(base: Path | str = "/mnt") -> list[Path]:
+    """Readable ``/mnt/<name>`` roots; probed with a timeout so a stale network mount cannot stall the browser."""
+    if platform.system() != "Linux":
+        return []
+    base_path = Path(base)
+    try:
+        resolved_base = base_path.resolve()
+        children = list(base_path.iterdir())
+    except (OSError, RuntimeError, ValueError):
+        return []
+    readable = _readable_dirs_within(
+        [str(c) for c in children if not is_sensitive_path_component(c.name)],
+        _DRIVE_PROBE_TIMEOUT_S,
+    )

P2 Badge Bound stale mount probes without leaking worker threads

When a stale NFS/CIFS mount under /mnt leaves os.path.isdir() blocked, every folder-browser request reaches this probe; _readable_dirs_within() returns after the timeout but deliberately leaves the daemon thread running. Because linux_external_mount_roots() is recomputed on every navigation, repeated requests accumulate blocked threads until the OS call returns—potentially indefinitely—and can eventually exhaust the process's thread/resource limit. Cache these probes or use a cancellable/non-threaded mount check rather than starting fresh unjoinable workers per request.

Useful? React with 👍 / 👎.

Accepted on the mirror

Reply from danielhanchen:

Fixed in cf39de3: a probe already stuck past the timeout is not restarted, so a hard-mounted stale share holds at most the threads started in its first timeout window.

Round 2: reviewed mirror 22ca62ab4 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] on studio/backend/utils/paths/external_media.py (on the mirror)

@@ -105,26 +140,88 @@ def linux_run_media_mount_roots(
     except (OSError, RuntimeError, ValueError):
         return []
     for volume_dir in volume_dirs:
-        if is_sensitive_path_component(volume_dir.name):
+        accepted = _accept_linux_volume(volume_dir, resolved_base, min_parts = 2, seen = seen)
+        if accepted is not None:
+            roots.append(accepted)
+    return roots
+
+
+def linux_media_mount_roots(
+    base: Path | str = "/media",
+    *,
+    user: str | None = None,
+    run_media: Path | str = "/run/media",
+) -> list[Path]:
+    """Readable ``/media/<user>/<volume>`` and legacy ``/media/<volume>`` roots; entries may symlink into *run_media*."""
+    if platform.system() != "Linux":
+        return []
+    user = user or _current_username()
+    base_path = Path(base)
+    try:
+        resolved_base = base_path.resolve()
+        children = list(base_path.iterdir())
+    except (OSError, RuntimeError, ValueError):
+        return []
+
+    roots: list[Path] = []
+    seen: set[str] = set()
+    for child in children:
+        if is_sensitive_path_component(child.name):
             continue
         try:
-            resolved = volume_dir.resolve()
+            if not child.is_dir():
+                continue
+            # The current user's folder is the parent of their mounts, not a volume.
+            volume_dirs = list(child.iterdir()) if user and child.name == user else [child]
         except (OSError, RuntimeError, ValueError):
             continue
-        if not _is_linux_media_mount_path(str(resolved), resolved_base):
-            continue
-        if _contains_sensitive_media_component(resolved, resolved_base):
-            continue
-        key = os.path.normcase(os.path.realpath(str(resolved)))
-        if key in seen:
-            continue
-        try:
-            is_dir = resolved.is_dir()
-        except OSError:
+        for volume_dir in volume_dirs:
+            accepted = _accept_linux_volume(
+                volume_dir, resolved_base, min_parts = 1, seen = seen, alias_root = run_media
+            )
+            if accepted is not None:
+                roots.append(accepted)
+    return roots
+
+
+def linux_mnt_mount_roots(base: Path | str = "/mnt") -> list[Path]:
+    """Readable ``/mnt/<name>`` roots; probed with a timeout so a stale network mount cannot stall the browser."""
+    if platform.system() != "Linux":
+        return []
+    base_path = Path(base)
+    try:
+        resolved_base = base_path.resolve()
+        children = list(base_path.iterdir())
+    except (OSError, RuntimeError, ValueError):
+        return []
+    readable = _readable_dirs_within(
+        [str(c) for c in children if not is_sensitive_path_component(c.name)],
+        _DRIVE_PROBE_TIMEOUT_S,
+    )
+    roots: list[Path] = []
+    seen: set[str] = set()
+    for child in children:
+        if str(child) not in readable:
             continue
-        if is_dir and os.access(resolved, os.R_OK | os.X_OK):
+        accepted = _accept_linux_volume(child, resolved_base, min_parts = 1, seen = seen)

P2 Badge Keep the post-probe mount checks bounded

When a /mnt child becomes stale after the timed probe succeeds, this call synchronously repeats resolve(), is_dir(), and os.access() inside _accept_linux_volume. Any of those filesystem operations can then block indefinitely on a hard NFS/CIFS mount, so linux_mnt_mount_roots() no longer provides the timeout promised by its docstring. Run the complete resolution/readability check inside the bounded probe, or reuse its validated result without performing another unbounded stat.

Useful? React with 👍 / 👎.

Rejected on the mirror

Reply from danielhanchen:

Not changing: this needs the mount to go stale in the microseconds between a probe that just answered and the immediate re-stat, which the kernel attribute cache from that probe normally serves; the probe bounds the realistic case (already-stale mount).

Round 3: reviewed mirror 22ca62ab4 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 22ca62ab43

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror 22ca62ab4 (no equivalent commit found on this PR).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

@danielhanchen
danielhanchen merged commit 42b7de3 into unslothai:main Oct 1, 2026
4 of 46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] - Support for Linux's Temporary Mounted Drives

3 participants