@@ -65,7 +65,6 @@ def graceful_signal_handler(signum, frame):
6565 )
6666 return
6767
68- task_logger .info (f"Received signal { signum } . Initiating graceful shutdown." )
6968 _shutdown_in_progress = True
7069
7170 # Let the default signal handler proceed, but our flag will prevent duplicate User.stop() calls
@@ -391,9 +390,6 @@ def get_next_prompt(self) -> Dict[str, Any]:
391390 self .environment .prompt_queue .put_nowait (prompt_data )
392391 return prompt_data
393392 else :
394- self .task_logger .warning (
395- "Prompt queue is empty or not initialized. Using default prompt."
396- )
397393 return {"id" : "default" , "prompt" : DEFAULT_PROMPT }
398394 except queue .Empty :
399395 self .task_logger .warning ("Prompt queue is empty. Using default prompt." )
@@ -419,21 +415,19 @@ def _log_token_counts(
419415 model_name = global_config .model_name or ""
420416 system_prompt = global_config .system_prompt or ""
421417
422- # Validate inputs
423418 user_prompt = user_prompt or ""
424419 reasoning_content = reasoning_content or ""
425420 model_output = model_output or ""
426421
427422 # Prefer usage_tokens if available and valid
428- completion_tokens = None
429- total_tokens = None
430-
431- if usage_tokens :
432- completion_tokens = usage_tokens .get ("completion_tokens" )
433- total_tokens = usage_tokens .get ("total_tokens" )
423+ completion_tokens = 0
424+ total_tokens = 0
434425
435426 # Fallback: manual counting if completion_tokens and total_tokens are missing
436- if completion_tokens is None or total_tokens is None :
427+ if usage_tokens and isinstance (usage_tokens , dict ):
428+ completion_tokens = usage_tokens .get ("completion_tokens" , 0 ) or 0
429+ total_tokens = usage_tokens .get ("total_tokens" , 0 ) or 0
430+ else :
437431 system_tokens = (
438432 count_tokens (system_prompt , model_name ) if system_prompt else 0
439433 )
@@ -453,9 +447,17 @@ def _log_token_counts(
453447 total_tokens = system_tokens + user_tokens + completion_tokens
454448
455449 # Ensure integer and log - only if tokens are not None and positive
456- if completion_tokens is not None and completion_tokens > 0 :
450+ if (
451+ completion_tokens
452+ and isinstance (completion_tokens , (int , float ))
453+ and completion_tokens > 0
454+ ):
457455 global_task_queue ["completion_tokens_queue" ].put (int (completion_tokens ))
458- if total_tokens is not None and total_tokens > 0 :
456+ if (
457+ total_tokens
458+ and isinstance (total_tokens , (int , float ))
459+ and total_tokens > 0
460+ ):
459461 global_task_queue ["all_tokens_queue" ].put (int (total_tokens ))
460462
461463 except Exception as e :
@@ -465,14 +467,14 @@ def _log_token_counts(
465467 def chat_request (self ):
466468 """Main Locust task that executes a single chat request."""
467469 global_config = get_global_config ()
468-
469- prompt_data = self .get_next_prompt ()
470-
470+ # Check if we need dataset mode (avoid unnecessary queue operations)
471+ needs_dataset = bool (
472+ global_config .test_data and global_config .test_data .strip ()
473+ )
474+ prompt_data = self .get_next_prompt () if needs_dataset else None
471475 base_request_kwargs , user_prompt = self .request_handler .prepare_request_kwargs (
472476 prompt_data
473477 )
474- # self.task_logger.info(f"base_request_kwargs: {base_request_kwargs}")
475-
476478 if not base_request_kwargs :
477479 self .task_logger .error (
478480 "Failed to generate request arguments. Skipping task."
@@ -490,7 +492,6 @@ def chat_request(self):
490492 if base_request_kwargs
491493 else "failure"
492494 )
493-
494495 try :
495496 if global_config .stream_mode :
496497 reasoning_content , model_output = (
@@ -505,17 +506,29 @@ def chat_request(self):
505506 )
506507 )
507508 except Exception as e :
508- self .task_logger .error (
509- f"Unhandled exception in chat_request: { e } " , exc_info = True
510- )
511- # Record the failure event for unhandled exceptions
512- response_time = (time .time () - start_time ) * 1000
509+ self .task_logger .error (f"Unhandled exception in chat_request: { e } " )
510+ # Record the failure event for unhandled exceptions with enhanced context
511+ try :
512+ response_time = (
513+ (time .time () - start_time ) * 1000 if start_time is not None else 0
514+ )
515+ except Exception :
516+ response_time = 0
517+
513518 ErrorHandler .handle_general_exception (
514519 f"Unhandled exception in chat_request: { e } " ,
515520 self .task_logger ,
516521 response = None ,
517522 response_time = response_time ,
518- request_name = request_name ,
523+ additional_context = {
524+ "stream_mode" : global_config .stream_mode ,
525+ "api_path" : global_config .api_path ,
526+ "prompt_preview" : (
527+ str (user_prompt )[:100 ] if user_prompt else "No prompt"
528+ ),
529+ "task_id" : global_config .task_id ,
530+ "request_name" : request_name ,
531+ },
519532 )
520533
521534 if reasoning_content or model_output or usage_tokens :
0 commit comments