Skip to content
Merged
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
26 changes: 14 additions & 12 deletions sgl-router/src/routers/grpc/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,20 +769,22 @@ impl GrpcRouter {

// Check if reasoning parsing is enabled and separate_reasoning is requested
if original_request.separate_reasoning {
if let Ok(mut parser) = self
let pooled_parser = self
.reasoning_parser_factory
.create(&original_request.model)
{
match parser.detect_and_parse_reasoning(&processed_text) {
Ok(result) => {
if !result.reasoning_text.is_empty() {
reasoning_text = Some(result.reasoning_text);
}
processed_text = result.normal_text;
}
Err(e) => {
return Err(format!("Reasoning parsing error: {}", e));
.get_pooled(&original_request.model);

let mut parser = pooled_parser
.lock()
.map_err(|e| format!("Failed to acquire reasoning parser lock: {}", e))?;
match parser.detect_and_parse_reasoning(&processed_text) {
Ok(result) => {
if !result.reasoning_text.is_empty() {
reasoning_text = Some(result.reasoning_text);
}
processed_text = result.normal_text;
}
Err(e) => {
return Err(format!("Reasoning parsing error: {}", e));
}
}
Comment on lines +776 to 789
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

To minimize the duration the mutex is held, it's a good practice to release the lock as soon as the shared resource is no longer needed. You can achieve this by scoping the lock acquisition and the call to detect_and_parse_reasoning within a block, and then processing the result outside of that block. This ensures the lock is not held during the match and subsequent assignments.

            let result = {
                let mut parser = pooled_parser
                    .lock()
                    .map_err(|e| format!("Failed to acquire reasoning parser lock: {}", e))?;
                parser.detect_and_parse_reasoning(&processed_text)
            };

            match result {
                Ok(result) => {
                    if !result.reasoning_text.is_empty() {
                        reasoning_text = Some(result.reasoning_text);
                    }
                    processed_text = result.normal_text;
                }
                Err(e) => {
                    return Err(format!("Reasoning parsing error: {}", e));
                }
            }

}
Expand Down
Loading