Skip to content

feat: configure shared memory for singleuser pods - #246

Open
Keerthirajan58 wants to merge 11 commits into
nebari-dev:mainfrom
Keerthirajan58:feat/singleuser-shared-memory
Open

Keerthirajan58 wants to merge 11 commits into
nebari-dev:mainfrom
Keerthirajan58:feat/singleuser-shared-memory

Conversation

@Keerthirajan58

@Keerthirajan58 Keerthirajan58 commented Sep 4, 2026 •

Copy link
Copy Markdown

The small /dev/shm described in #54 can cause PyTorch DataLoader workers to
crash and Ray to fall back to disk. This adds a memory-backed mount for singleuser
pods, with an 8 GiB cap that operators can change through singleuser.sharedMemory.
Profiles can set their own limit with shm_size_limit. If either size is empty
or not a string, the hub logs a warning and falls back instead of failing to
start: a profile uses the chart-wide size, and the chart-wide size uses 8 GiB.

I've kept the existing home and config volumes unchanged and added the
shared-memory entries to those lists. The enabled switch lets operators keep
the runtime default or use a mount they already define through profile overrides.
Profiles never get a second shared-memory volume or mount. shm_size_limit
changes the size of the chart's dshm volume, and if a profile mounts something
else at /dev/shm, the chart's mount replaces it unless enabled is false.

Testing

In September I ran the shared-memory test in kind using the chart's notebook
image at the time (sha-740eafb). It successfully wrote a 128 MiB shared-memory
segment. A separate pod using the same image without the new mount had the usual
64 MiB /dev/shm and failed the same write with SIGBUS.

That run caught a mistake in my original test: I expected the filesystem to be
exactly 8 GiB, but Kubernetes capped it at the smaller node's memory capacity.
The test now checks the configured cap separately from the filesystem capacity
and still requires the full 128 MiB write.

All 242 unit tests pass on Python 3.10, including checks that render the Helm
values and generate pods through KubeSpawner. Ruff, Helm lint, the nine docs
tests, the docs build, and internal links also pass.

For the local kind run, I disabled unrelated NFS shared storage because its test
image is AMD64-only and this machine is ARM64. I ran only the shared-memory e2e
test locally; the full suite still needs to run in CI.

Closes #54.

@pmeier pmeier 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.

Thanks @Keerthirajan58 for the PR. I've added some high level comments that we need to address before going any further.

Comment thread values.yaml Outdated
# avoids the container runtime's 64 MiB default, which can make Ray fall back
# to disk and can crash multiprocessing PyTorch DataLoader workers.
sharedMemory:
enabled: true

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.

When would someone disable this?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

An operator might already define a /dev/shm mount through their profile's
volume overrides. Turning this off lets them keep that mount without the chart
replacing it. I've added a test for that case and explained it in the docs.
Without a custom mount, disabling it leaves the runtime's default in place.

Comment thread config/jupyterhub/01-spawner.py Resolved
@Keerthirajan58

Copy link
Copy Markdown
Author

@pmeier, I've pushed the changes discussed above and brought the branch up to date
with main. All 184 unit tests pass, including a check that the shared-memory
override works with the new GPU image profiles.

I also tested it in kind. The notebook wrote 128 MiB successfully, while the same
image with the default 64 MiB mount failed the write. The updated description has
the details, including the NFS setting I disabled for the ARM64 run. Could you
take another look and check if it works? If it does, you can approve the workflow runs when you have a chance.

Sprig's quote renders nil as nothing, so `sizeLimit: null` left
`"shared-memory-size-limit": ,` in 00-chart-derived.py and the hub failed
with a SyntaxError, even with shared memory disabled. Default it to "" like
the neighbouring shutdown-no-activity-timeout key. When enabled, the
spawner now stops with its own "must be a non-empty string" error; when
disabled, the hub loads normally.
An invalid per-profile shm_size_limit raised inside _filter_profiles, which
runs on every profile-list render. One bad value broke the spawn page and
spawns for every user who could see that profile, and the translation and
its warnings repeated on each render.

Translate profiles once when _profiles is built, next to
_resolve_image_variants, and follow that function's rule: a bad value logs
a warning naming the profile and falls back to the chart-wide size.

Also drop a test for singleuser.extraVolumes, a key 01-spawner.py never
reads, and stop a test comment from claiming when overrides are reapplied.
The shared-memory constants, helpers and settings sat between the Storage
comment and the storage code it explains. Move them into a "Shared memory"
section after the base volume lists, next to the append that uses them.
Build the dshm volume and mount in one place so the base append and the
profile overrides share the same definition. No behavior change.
List sharedMemory after networkPolicy in values.yaml so the block's
network-access header stays next to the entries it describes, and replace
the long values-reference paragraph with a link to the Shared memory
section of server-profiles.md, which already covers it.
An empty, null or non-string singleuser.sharedMemory.sizeLimit raised
ValueError when 01-spawner.py loaded, which stopped the hub. Other bad
deployer values in this file only warn: the code-server idle timeout,
_resolve_image_variants, and a profile's shm_size_limit.

Do the same for the chart-wide size: log a warning that names
singleuser.sharedMemory.sizeLimit and use the chart default of 8Gi.
One helper now checks both the chart-wide size and a profile's
shm_size_limit, so the two paths follow the same rule. A null sizeLimit
is still ignored when shared memory is disabled.
Assert that a disabled chart still warns when a profile sets
shm_size_limit, and run the invalid profile size test with a chart-wide
size other than 8Gi so it shows the profile falls back to that size
rather than the chart default.

Also name shm_size_limit and _translate_profile_shared_memory in the
comment that lists the chart-only profile keys.
The chart-wide size is only validated when shared memory is enabled.
Assert that a disabled chart with sizeLimit null logs no sizeLimit
warning, so moving the validation outside that check fails a test.
@Keerthirajan58

Copy link
Copy Markdown
Author

@pmeier, I merged the latest main to resolve the conflict with #226. Both PRs added entries to _CHART_DERIVED and tests at the end of test_chart_derived.py, so I kept both.

While doing that I found a bug in my own change. #226 guards its new value against null and my sizeLimit line did not, so sizeLimit: null rendered a syntax error into the hub config. It now renders an empty string, and an empty or non-string size logs a warning and falls back to 8Gi, so a bad value no longer stops the hub, in line with #226's guard and _resolve_image_variants. A bad per-profile shm_size_limit also used to raise on every profile-list render, which broke the options form for everyone who could see that profile. Profiles are now translated once at load and fall back to the chart-wide size with a warning.

I also moved the shared-memory helpers into their own section, so the storage code reads the same as on main, and removed the leftover singleuser.extraVolumes test. All 242 unit tests pass on Python 3.10, along with Ruff, Helm lint and the docs build. I also ran #260's pre-spawn hook locally against the rendered chart, and the pod still gets exactly one /dev/shm mount.

The September workflow runs expired before they were approved, so the runs for this push need approval. If you'd rather always mount /dev/shm, I can drop the enabled toggle. Could you take another look when you have time?

This branch has not been deployed

No deployments
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.

Configure adequately sized /dev/shm for singleuser pods to avoid silent failures in Ray, PyTorch DataLoader, and other shm-using libraries

2 participants