fix(backend): don't crash the whole process on an invalid cutstrings/extract_regex (#10855)

Finetune() compiled every model cutstrings/extract_regex entry via regexp.Compile
and called xlog.Fatal on failure, which terminates the entire local-ai process.
A single model config with an invalid regex (e.g. cutstrings: ["("]) turns one
/v1/chat/completions request into a process-level denial of service.

Log the compile error and skip the offending pattern instead. The mutex is
released before continuing, and skipping avoids dereferencing the nil regexp
that removing the fatal would otherwise leave behind.

Fixes #10843

Signed-off-by: Tai An <antai12232931@outlook.com>
This commit is contained in:
Tai An
2026-07-15 23:54:35 -07:00
committed by GitHub
parent ff8774327f
commit 5fe48e4910

View File

@@ -463,7 +463,9 @@ func Finetune(config config.ModelConfig, input, prediction string) string {
if !ok {
r, err := regexp.Compile(c)
if err != nil {
xlog.Fatal("failed to compile regex", "error", err)
mu.Unlock()
xlog.Error("failed to compile cutstrings regex, skipping", "error", err, "regex", c)
continue
}
cutstrings[c] = r
reg = cutstrings[c]
@@ -480,7 +482,9 @@ func Finetune(config config.ModelConfig, input, prediction string) string {
if !ok {
regex, err := regexp.Compile(r)
if err != nil {
xlog.Fatal("failed to compile regex", "error", err)
mu.Unlock()
xlog.Error("failed to compile extract_regex regex, skipping", "error", err, "regex", r)
continue
}
cutstrings[r] = regex
reg = regex