@@ -625,7 +625,8 @@ def _build_rollout(self, trust_remote_code=False):
625625 # For sync mode, we directly switch to trainer mode here.
626626 # For async mode, we can't call run_until_complete here, so we will switch to trainer mode in AgentLoopManager.
627627 if rollout_config .mode == "sync" and self ._is_actor :
628- asyncio .run (self .trainer_mode ())
628+ loop = asyncio .get_event_loop ()
629+ loop .run_until_complete (self .trainer_mode ())
629630
630631 async def rollout_mode (self ):
631632 """Context switch hybridengine to rollout mode."""
@@ -901,14 +902,19 @@ def generate_sequences(self, prompts: DataProto):
901902
902903 timing_generate = {}
903904 if self ._is_actor : # For rollout only, we do not switch context.
904- asyncio .run (self .rollout_mode ())
905+ try :
906+ loop = asyncio .get_event_loop ()
907+ except RuntimeError :
908+ loop = asyncio .new_event_loop ()
909+ asyncio .set_event_loop (loop )
910+ loop .run_until_complete (self .rollout_mode ())
905911 log_gpu_memory_usage ("After switch to rollout mode" , logger = logger )
906912
907913 with simple_timer ("generate_sequences" , timing_generate ):
908914 output = self .rollout .generate_sequences (prompts = prompts )
909915
910916 if self ._is_actor :
911- asyncio . run (self .trainer_mode ())
917+ loop . run_until_complete (self .trainer_mode ())
912918 log_gpu_memory_usage ("After switch to trainer mode" , logger = logger )
913919
914920 # We calculate the average timing across all ranks
0 commit comments