Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Excessive CPU and memory usage caused by a wrong syntax (unclosed brackets) #1005

Open
cykuotw opened this issue Nov 25, 2024 · 1 comment · May be fixed by #1029
Open

Excessive CPU and memory usage caused by a wrong syntax (unclosed brackets) #1005

cykuotw opened this issue Nov 25, 2024 · 1 comment · May be fixed by #1029
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@cykuotw
Copy link

cykuotw commented Nov 25, 2024

There is probably an unexpected situation due to unclosed brackets that trigger excessive CPU and memory usage in templ fmt and templ generate.

I have a templ component defined as

templ SplitRule(user types.GroupMember, members []types.GroupMember)

When I call it in another templ file but accidentally forget to close the brackets like:

@SplitRule(types.GroupMember{
    UserID:   uuid.NewString(),
    Username: "user me",
}, []types.GroupMember{
    {
    UserID:   uuid.NewString(),
    Username: "user 1",
    },
// })  <------ should be here but I accidentally forget

There are two cases that can trigger the excessive CPU and memory usage:

  1. save the code in VSCode and trigger templ fmt
    • resolve: disable/uninstall templ-vscode vscode plugin and reload vscode
  2. use templ generate
    • resolve: kill terminal process

Both of the 2 latest versions can reproduce the problem.

Just in case you want to reproduce it, here is the repo and here is the file

Snapshots

  • top -i output,
    image
  • templ generate -v output
    image

templ info output

(✓) os [ goos=linux goarch=amd64 ]
(✓) go [ location=/usr/local/go/bin/go version=go version go1.23.3 linux/amd64 ]
(✓) gopls [ location=/home/cykuotw/go/bin/gopls version=golang.org/x/tools/gopls v0.16.2 ]
(✓) templ [ location=/home/cykuotw/go/bin/templ version=v0.2.793 ]

Desktop:

  • OS: WSL Ubuntu 20.04.6 LTS
  • templ CLI version: v0.2.793
  • Go version: go1.23.3 linux/amd64
  • gopls version: golang.org/x/tools/gopls v0.16.2
@a-h
Copy link
Owner

a-h commented Dec 28, 2024

Thanks for the report. Likely an issue with the parser.

The Go expression parser is here: https://github.com/a-h/templ/tree/main/parser/v2/goexpression
And this is the part that uses the Go parser to parse Go code within templ @SplitRule(...):

if r.Expression, err = parseGo("templ element", pi, goexpression.TemplExpression); err != nil {

It should be straightforward to add a test to https://github.com/a-h/templ/blob/main/parser/v2/templelementparser_test.go in order to find it, followed by using the profiler to work out where it's getting stuck.

@a-h a-h added bug Something isn't working good first issue Good for newcomers labels Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants