@@ -199,7 +199,14 @@ def __init__(
199199 sizing_mode = "stretch_width" ,
200200 )
201201
202- num_gui_sessions = len (list_gui_sessions ())
202+ # `on_session_created` in setup.py runs *before* this constructor and has
203+ # already added this session's log file to the count. So `list_gui_sessions()`
204+ # returns ALL sessions including the one being admitted right now. Subtract 1
205+ # to get the count of OTHER (already-active) sessions, which is what
206+ # `can_admit_new_session()` expects ("how many sessions are already
207+ # consuming a slot — can we fit one more on top of them?").
208+ all_gui_sessions = len (list_gui_sessions ())
209+ existing_gui_sessions = max (0 , all_gui_sessions - 1 )
203210 ram_percent = get_container_used_memory () / get_container_total_memory () * 100
204211 total_ram_bytes = get_container_total_memory ()
205212
@@ -235,12 +242,10 @@ def __init__(
235242 self ._preloaded_analyzer = None
236243 print (f"Could not pre-load analyzer for size estimate: { e } . Using static fallback." )
237244
238- # NB: this is the entry point for THIS session, so it isn't counted in
239- # num_gui_sessions yet — can_admit_new_session predicts what RAM would
240- # look like AFTER this admit and rejects if it'd cross the safe ceiling
241- # or the hard cap.
245+ # Pass the count of OTHER existing sessions (not this one) so
246+ # can_admit_new_session can answer "is there room for one more?".
242247 if not can_admit_new_session (
243- current_count = num_gui_sessions ,
248+ current_count = existing_gui_sessions ,
244249 used_pct = ram_percent ,
245250 estimate_pct = estimate_pct ,
246251 ):
@@ -255,8 +260,8 @@ def __init__(
255260 hard_cap = get_hard_cap_sessions ()
256261 safe_max = get_safe_max_ram_pct ()
257262 effective_estimate = estimate_pct if estimate_pct is not None else get_per_session_estimate_pct ()
258- if num_gui_sessions >= hard_cap :
259- reason = f"hard session cap reached ({ num_gui_sessions } /{ hard_cap } )"
263+ if existing_gui_sessions >= hard_cap :
264+ reason = f"hard session cap reached ({ existing_gui_sessions } /{ hard_cap } )"
260265 else :
261266 source = f"{ estimate_units } units" if estimate_units is not None else "static fallback"
262267 reason = (
0 commit comments