@@ -435,12 +435,20 @@ async def repositories_from_session(
435435 return repositories_from_project (project , git_providers )
436436
437437
438- def get_culling (resource_pool : ResourcePool , nb_config : NotebooksConfig ) -> Culling :
438+ def get_culling (
439+ user : AuthenticatedAPIUser | AnonymousAPIUser , resource_pool : ResourcePool , nb_config : NotebooksConfig
440+ ) -> Culling :
439441 """Create the culling specification for an AmaltheaSession."""
440442 idle_threshold_seconds = resource_pool .idle_threshold or nb_config .sessions .culling .registered .idle_seconds
441- hibernation_threshold_seconds = (
442- resource_pool .hibernation_threshold or nb_config .sessions .culling .registered .hibernated_seconds
443- )
443+ if user .is_anonymous :
444+ # NOTE: Anonymous sessions should not be hibernated at all, but there is no such option in Amalthea
445+ # So in this case we set a very low hibernation threshold so the session is deleted quickly after
446+ # it is hibernated.
447+ hibernation_threshold_seconds = 1
448+ else :
449+ hibernation_threshold_seconds = (
450+ resource_pool .hibernation_threshold or nb_config .sessions .culling .registered .hibernated_seconds
451+ )
444452 return Culling (
445453 maxAge = format_duration (timedelta (seconds = nb_config .sessions .culling .registered .max_age_seconds )),
446454 maxFailedDuration = format_duration (timedelta (seconds = nb_config .sessions .culling .registered .failed_seconds )),
@@ -529,7 +537,7 @@ async def patch_session(
529537 patch .spec .affinity = node_affinity_from_resource_class (rc , nb_config .sessions .affinity_model )
530538 # Priority class (if a quota is being used)
531539 patch .spec .priorityClassName = rc .quota
532- patch .spec .culling = get_culling (rp , nb_config )
540+ patch .spec .culling = get_culling (user , rp , nb_config )
533541
534542 # If the session is being hibernated we do not need to patch anything else that is
535543 # not specifically called for in the request body, we can refresh things when the user resumes.
0 commit comments