Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions python/sglang/srt/managers/tokenizer_communicator_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,20 @@ async def update_weights_from_ipc(
self.server_args.dp_size == 1 or self.server_args.enable_dp_attention
), "dp_size must be 1 or dp attention must be enabled for update weights from IPC"
logger.info("Starting IPC weight update")
# This means that weight sync cannot run while requests are in progress.
async with self.model_update_lock.writer_lock:

# Skip the writer lock when paused: readers are blocked on
# is_pause_cond so no concurrent inference can race, and
# waiting for the writer lock would deadlock because existing
# readers are stuck waiting on the paused scheduler.
async with self.is_pause_cond:
is_paused = self.is_pause

lock_context = (
self.model_update_lock.writer_lock
if not is_paused
else nullcontext()
)
async with lock_context:
result = (await self.update_weights_from_ipc_communicator(obj))[0]
success, message = result.success, result.message
Comment on lines +606 to 620

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The current implementation introduces a race condition. By releasing is_pause_cond before performing the weight update with nullcontext(), it's possible for continue_generation to be called in the interim. This would set is_pause = False, allowing new inference requests to proceed and the scheduler to unpause. This leads to concurrent weight updates and inference, which can cause data corruption or crashes.

To fix this, the weight update should be performed while holding the is_pause_cond lock if the engine is paused, ensuring that continue_generation cannot unpause the scheduler until the update is complete.

Suggested change
# Skip the writer lock when paused: readers are blocked on
# is_pause_cond so no concurrent inference can race, and
# waiting for the writer lock would deadlock because existing
# readers are stuck waiting on the paused scheduler.
async with self.is_pause_cond:
is_paused = self.is_pause
lock_context = (
self.model_update_lock.writer_lock
if not is_paused
else nullcontext()
)
async with lock_context:
result = (await self.update_weights_from_ipc_communicator(obj))[0]
success, message = result.success, result.message
async with self.is_pause_cond:
is_paused = self.is_pause
if is_paused:
# Skip the writer lock when paused to avoid deadlock with existing readers.
# We hold is_pause_cond during the update to prevent continue_generation
# from unpausing the scheduler and allowing new readers to race.
result = (await self.update_weights_from_ipc_communicator(obj))[0]
success, message = result.success, result.message
if not is_paused:
# This means that weight sync cannot run while requests are in progress.
async with self.model_update_lock.writer_lock:
result = (await self.update_weights_from_ipc_communicator(obj))[0]
success, message = result.success, result.message

except Exception as e:
Expand All @@ -624,7 +636,14 @@ async def post_process_weights(
) -> Tuple[bool, str]:
"""Trigger post-processing hooks for weights after loading (e.g., Marlin conversion)."""
self.auto_create_handle_loop()
async with self.model_update_lock.writer_lock:

async with self.is_pause_cond:
is_paused = self.is_pause

lock_context = (
self.model_update_lock.writer_lock if not is_paused else nullcontext()
)
async with lock_context:
results = await self.post_process_weights_communicator(obj)
return _Communicator.merge_results(results)
Comment on lines +640 to 648

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Similar to the change in update_weights_from_ipc, this implementation is susceptible to a race condition where continue_generation could unpause the engine after is_paused is checked but before the update completes. Holding the is_pause_cond lock during the update when paused prevents this race.

Suggested change
async with self.is_pause_cond:
is_paused = self.is_pause
lock_context = (
self.model_update_lock.writer_lock if not is_paused else nullcontext()
)
async with lock_context:
results = await self.post_process_weights_communicator(obj)
return _Communicator.merge_results(results)
async with self.is_pause_cond:
is_paused = self.is_pause
if is_paused:
results = await self.post_process_weights_communicator(obj)
if not is_paused:
async with self.model_update_lock.writer_lock:
results = await self.post_process_weights_communicator(obj)
return _Communicator.merge_results(results)


Expand Down
Loading