Skip to content

Fix hang when doing s += None. - #1297

Merged
merrymercy merged 1 commit into
sgl-project:mainfrom
max99x:main
Sep 2, 2024
Merged

merrymercy merged 1 commit into
sgl-project:mainfrom
max99x:main

Conversation

@max99x

@max99x max99x commented Sep 2, 2024

Copy link
Copy Markdown
Contributor

Motivation

SGLang will terminate the worker thread if you append a None to a ProgramState because None is used as a sentinel to signal the executor to terminate. This results in the program silently hanging when anything that waits on the worker is read from the state.

Repro script:

@sglang.function
def f(s):
    s += sglang.user('What is the capital of Iceland?')
    s += sglang.assistant(sglang.gen('result', max_tokens=256))
    s += None

state = f.run()
for m in state.messages():
    print(m["role"], ":", m["content"])

Modifications

Adding None to a ProgramState will now raise a ValueError.

Checklist

  • [✅] Format your code according to the Contributor Guide.
  • [N/A] Add unit tests as outlined in the Contributor Guide.
  • [N/A] Update documentation as needed, including docstrings or example tutorials.

@merrymercy
merrymercy merged commit 6def9b0 into sgl-project:main Sep 2, 2024
timethink pushed a commit to timethink/sglang that referenced this pull request Mar 9, 2025
efschu pushed a commit to efschu/htsglang that referenced this pull request Sep 9, 2026
… above every branch of `span`

Boot weg2dec2 (9b40973) died in group D's decode CUDA-graph capture with
`RecursionError: maximum recursion depth exceeded`, `parallel_state.py:1444
return self.all_reduce(input_)` repeated 2966 times
(gpu-arb/weg2/BOOT_weg2dec2_0909.md). Root, confirmed at file:line:

  collective_clock.py:401  return self._slot is not None or self._capture is not None
  parallel_state.py:1438   if _COLLECTIVE_CLOCK.armed:
  parallel_state.py:1443       with _COLLECTIVE_CLOCK.span(...):
  parallel_state.py:1444           return self.all_reduce(input_)

The dispatch sites do not merely GATE on `armed`, they RE-ENTER THEMSELVES
inside the span and rely on it being false the second time, so the body runs
once. 994f0b5 (#1241b, not 6146974 as the boot record reads -- same
series, wrong commit) widened `armed` from `_slot is not None` to the OR of
two fields, while `span` kept FOUR separate narrower disarms:

  * capture branch cleared `_capture` only  -> `_slot` kept `armed` true
  * slot path cleared `_slot` only          -> `_capture` kept `armed` true
  * `graph_capture_skipped` early return    -> cleared NOTHING
  * `slot is None` early return             -> cleared NOTHING

Four reachable (scope x capturing x slot) states therefore never went
disarmed and recursed until the stack ended. The widened `armed` is an OR;
the disarms were not. The two early returns were correct before #1241b,
because `armed` could not be true from `_capture`.

FIX: hoist ONE disarm of BOTH fields above every branch, restore both in one
`finally`. No new flag and no new field -- the disarm is now EQUAL to `armed`
by construction rather than by four correct recollections, and a third scope
has to be added in one place instead of remembered in four. `armed`'s
docstring states that contract so the next widening carries it.

The non-capturing paths are unchanged in behaviour: on them `_capture` is
already None, so clearing and restoring it is a no-op, and the slot path
acquires the same events, appends the same (start, end, family), and touches
the same counters in the same order.

RED-FIRST, hermetic, no CUDA (`ClockBackend` fake, `is_capturing()` True).
Every existing test in the file calls `span` DIRECTLY; none models the
dispatch site's re-entry, which is exactly why the suite was green on the
commit that died. `ReentrantDispatch` models parallel_state.py:1438-1444
with no depth cap, so the defect's signature stays `RecursionError`.

TALLIES (remote desk cachyllama, junit-authoritative, -n 8 -p no:randomly;
local box unavailable: gpuq booking y9jyp8 owner kvtail2 running on 0,1,2):

  parent 9b40973 + tests only (ce8393f531): tests=1439 failures=32 errors=0
  fixed              (1627ba11aa):             tests=1439 failures=27 errors=0
  NEW failures 0; the 5 that flipped are exactly the new ones; identical
  1359-node set both runs. 27 pre-existing failures unchanged (9 in
  test_collective_census_broadcast_families_583, 18 across six weg2 files).
  Red shape on the parent: 4x RecursionError + 1x `armed` still true inside
  the span.

  Files: test_decode_graph_event_nodes_1241b, test_decode_round_clock_1241,
  test_collective_census_broadcast_families_583, test_pp_bubble_1236,
  test_rank_prefill_log, test_kvso_tick_graph_614, test/registered/unit/weg2/

MUTANTS, all on the danger direction, all red:
  M1 early return re-arms the slot (its pre-sgl-project#1297 disarm dropped)  -> 1 failure
  M2 `_capture` dropped from the hoisted disarm                    -> 6 failures
  M3 `armed` reads `_capture` only, `_slot` stays live             -> 4 failures

ruff check clean on both files; ruff format reports 0 hunks in the added
lines (the 3 + 4 hunks it does report are byte-identical on 9b40973).

UNPROVEN: no boot. This is DESK-PROVEN only -- the recursion is reproduced
and closed hermetically, but group D has not reached READY on metal since.
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.

2 participants