1Panel-dev/1Panel · error
unexpected end of file while scanning a string, maybe an unc
Error message
unexpected end of file while scanning a string, maybe an unclosed lua code?
What it means
Panic in the nginx-config lexer's scanLuaCode when EOF is hit while scanning a *_by_lua_block { ... } region — brace nesting tracked on a rune stack never closed, so the scanner ran off the end of the input. It is a hard parse error for unbalanced lua blocks.
Source
Thrown at agent/utils/nginx/parser/lexer.go:91
return s.scanComment()
case ch == '$':
return s.scanVariable()
case isQuote(ch):
return s.scanQuotedString(ch)
default:
return s.scanKeyword()
}
}
func (s *lexer) scanLuaCode() flag.Flag {
ret := s.NewToken(flag.LuaCode)
stack := make([]rune, 0, 50)
code := strings.Builder{}
for {
ch := s.read()
if ch == rune(flag.EOF) {
panic("unexpected end of file while scanning a string, maybe an unclosed lua code?")
}
if ch == '#' {
code.WriteRune(ch)
code.WriteString(s.readUntil(isEndOfLine))
continue
} else if ch == '}' {
if len(stack) == 0 {
_ = s.reader.UnreadRune()
return ret.Lit(strings.TrimRight(strings.Trim(code.String(), "\n"), "\n "))
}
if stack[len(stack)-1] == '{' {
stack = stack[0 : len(stack)-1]
}
} else if ch == '{' {
stack = append(stack, ch)
}
code.WriteRune(ch)
}View on GitHub (pinned to 5ac7c80881)
Solutions
- Open the file reported by the parse operation and balance the braces in the *_by_lua_block section (count { vs })
- Validate before saving: nginx -t -c <file> or re-parse via the same agent parser in a test
- Restore the previous config version from 1Panel's config history if the edit broke it
Example fix
# before
access_by_lua_block {
if x then
do_thing()
# after
access_by_lua_block {
if x then
do_thing()
end
} Defensive patterns
Strategy: validation
Validate before calling
python3 - <<'PY'
import sys
txt = open(sys.argv[1]).read()
assert txt.count('{') == txt.count('}'), f"unbalanced braces: {{={txt.count('{')} }}={txt.count('}')}"
print('braces balanced')
PY /opt/1panel/apps/openresty/openresty/conf/nginx.conf Prevention
- Balance-check braces (especially inside *_by_lua_block) before saving configs
- Keep lua snippets small and test them in a standalone file first
When it happens
Trigger: A config contains access_by_lua_block { ... with a missing closing '}' (or nested unbalanced braces inside the lua code), and the agent parses that file with agent/utils/nginx/parser.
Common situations: Hand-written WAF/rate-limit lua snippets with an extra '{' or a deleted '}'; truncated config after an interrupted save; copy-paste that dropped the last line.
Related errors
- unexpected end of file while scanning a string, maybe an unc
- no enough parameter for location
- unexpected token %s (%s) on line %d, column %d
- Fatal error config file: %s \n
- init db dir failed, err: %v
AI-assisted analysis of 1Panel-dev/1Panel@5ac7c80881 (2026-08-15).
Data as JSON: /api/errors/c074c84f6711d17d.
Report an issue: GitHub.