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

On tag/branch-exist check, dont panic if repo is nil #21787

Merged
merged 2 commits into from Dec 4, 2022

Conversation

6543
Copy link
Member

@6543 6543 commented Nov 12, 2022

fix a panic found in gitea logs

@6543
Copy link
Member Author

6543 commented Nov 12, 2022

-> #21788, #21789

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Nov 12, 2022
@6543 6543 added backport/done All backports for this PR have been created and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Nov 12, 2022
@lunny
Copy link
Member

lunny commented Nov 13, 2022

It's better to do the check out of the method.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Nov 13, 2022
@6543
Copy link
Member Author

6543 commented Nov 13, 2022

you mean check what func did call this in the first place?

@6543
Copy link
Member Author

6543 commented Nov 13, 2022

2022/11/12 18:13:48 ...common/middleware.go:71:1() [E] [636fd44c-60] PANIC: runtime error: invalid memory address or nil pointer dereference
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/runtime/panic.go:220 (0x45061c)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/runtime/signal_unix.go:818 (0x4505ec)
        /home/build/build-deploy-gitea/build/gitea/modules/git/repo_branch_nogogit.go:43 (0xc6d1a0)
        /home/build/build-deploy-gitea/build/gitea/modules/git/repo_branch_nogogit.go:60 (0xc6b4c4)
        /home/build/build-deploy-gitea/build/gitea/modules/git/repo_branch.go:80 (0xc6b480)
        /home/build/build-deploy-gitea/build/gitea/routers/api/v1/repo/branch.go:58 (0x1fbd57e)
        /home/build/build-deploy-gitea/build/gitea/modules/web/wrap_convert.go:63 (0x1f3cb56)
        /home/build/build-deploy-gitea/build/gitea/modules/web/wrap.go:41 (0x1f3b009)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea-bin/pkg/mod/[github.com/go-chi/chi/v5@v5.0.7/mux.go:442](http://github.com/go-chi/chi/v5@v5.0.7/mux.go:442) (0x1723b75)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea/modules/web/wrap.go:98 (0x1f3bb94)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea/modules/web/wrap.go:98 (0x1f3bb94)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea/modules/context/api.go:277 (0x1b00651)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea/routers/api/v1/api.go:1174 (0x20174b3)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea-bin/pkg/mod/[github.com/go-chi/chi/v5@v5.0.7/mux.go:71](http://github.com/go-chi/chi/v5@v5.0.7/mux.go:71) (0x17219ac)
        /home/build/build-deploy-gitea/build/gitea-bin/pkg/mod/[github.com/go-chi/chi/v5@v5.0.7/mux.go:314](http://github.com/go-chi/chi/v5@v5.0.7/mux.go:314) (0x172335b)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea-bin/pkg/mod/[github.com/go-chi/chi/v5@v5.0.7/mux.go:442](http://github.com/go-chi/chi/v5@v5.0.7/mux.go:442) (0x1723b75)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea/routers/common/middleware.go:79 (0x1fa8742)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea/modules/web/routing/logger_manager.go:123 (0x1f36d2f)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea-bin/pkg/mod/[github.com/go-chi/chi/v5@v5.0.7/middleware/strip.go:30](http://github.com/go-chi/chi/v5@v5.0.7/middleware/strip.go:30) (0x1fa5eb8)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea-bin/pkg/mod/[github.com/chi-middleware/proxy@v1.1.1/middleware.go:37](http://github.com/chi-middleware/proxy@v1.1.1/middleware.go:37) (0x1fa27b6)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea/routers/common/middleware.go:32 (0x1fa8592)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea-bin/pkg/mod/[github.com/go-chi/chi/v5@v5.0.7/mux.go:88](http://github.com/go-chi/chi/v5@v5.0.7/mux.go:88) (0x1721961)
        /home/build/build-deploy-gitea/build/gitea/modules/web/route.go:200 (0x1f3a44d)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2916 (0x926efa)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:1966 (0x9223b6)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/runtime/asm_amd64.s:1571 (0x46d5c0)

Copy link
Contributor

@zeripath zeripath left a comment

Choose a reason for hiding this comment

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

whilst the repo should not be nil I suspect it may just be easier to have this kind of NPE protection.

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Nov 13, 2022
@zeripath
Copy link
Contributor

@6543 the logging surrounding this PANIC would have been helpful as

>         /home/build/build-deploy-gitea/build/gitea/modules/web/routing/logger_manager.go:123 (0x1f36d2f)

Would have written a start event at line 122 and an end event in its defer.


However, we can see

>         /home/build/build-deploy-gitea/build/gitea/routers/api/v1/repo/branch.go:58 (0x1fbd57e)

this is related to GetBranch in the API. Which is called once and once only line 836 in :

					m.Get("/*", repo.GetBranch)

The context of which is:

				m.Group("/branches", func() {
					m.Get("", repo.ListBranches)
					m.Get("/*", repo.GetBranch)
					m.Delete("/*", reqRepoWriter(unit.TypeCode), repo.DeleteBranch)
					m.Post("", reqRepoWriter(unit.TypeCode), bind(api.CreateBranchRepoOption{}), repo.CreateBranch)
				}, context.ReferencesGitRepo(), reqRepoReader(unit.TypeCode))

And we see that the gitRepo should not be nil here.

So I don't understand how this can happen.

@6543
Copy link
Member Author

6543 commented Nov 14, 2022

right! but it did :/

@lunny
Copy link
Member

lunny commented Nov 14, 2022

2022/11/12 18:13:48 ...common/middleware.go:71:1() [E] [636fd44c-60] PANIC: runtime error: invalid memory address or nil pointer dereference
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/runtime/panic.go:220 (0x45061c)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/runtime/signal_unix.go:818 (0x4505ec)
        /home/build/build-deploy-gitea/build/gitea/modules/git/repo_branch_nogogit.go:43 (0xc6d1a0)
        /home/build/build-deploy-gitea/build/gitea/modules/git/repo_branch_nogogit.go:60 (0xc6b4c4)
        /home/build/build-deploy-gitea/build/gitea/modules/git/repo_branch.go:80 (0xc6b480)
        /home/build/build-deploy-gitea/build/gitea/routers/api/v1/repo/branch.go:58 (0x1fbd57e)
        /home/build/build-deploy-gitea/build/gitea/modules/web/wrap_convert.go:63 (0x1f3cb56)
        /home/build/build-deploy-gitea/build/gitea/modules/web/wrap.go:41 (0x1f3b009)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea-bin/pkg/mod/[github.com/go-chi/chi/v5@v5.0.7/mux.go:442](http://github.com/go-chi/chi/v5@v5.0.7/mux.go:442) (0x1723b75)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea/modules/web/wrap.go:98 (0x1f3bb94)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea/modules/web/wrap.go:98 (0x1f3bb94)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea/modules/context/api.go:277 (0x1b00651)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea/routers/api/v1/api.go:1174 (0x20174b3)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea-bin/pkg/mod/[github.com/go-chi/chi/v5@v5.0.7/mux.go:71](http://github.com/go-chi/chi/v5@v5.0.7/mux.go:71) (0x17219ac)
        /home/build/build-deploy-gitea/build/gitea-bin/pkg/mod/[github.com/go-chi/chi/v5@v5.0.7/mux.go:314](http://github.com/go-chi/chi/v5@v5.0.7/mux.go:314) (0x172335b)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea-bin/pkg/mod/[github.com/go-chi/chi/v5@v5.0.7/mux.go:442](http://github.com/go-chi/chi/v5@v5.0.7/mux.go:442) (0x1723b75)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea/routers/common/middleware.go:79 (0x1fa8742)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea/modules/web/routing/logger_manager.go:123 (0x1f36d2f)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea-bin/pkg/mod/[github.com/go-chi/chi/v5@v5.0.7/middleware/strip.go:30](http://github.com/go-chi/chi/v5@v5.0.7/middleware/strip.go:30) (0x1fa5eb8)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea-bin/pkg/mod/[github.com/chi-middleware/proxy@v1.1.1/middleware.go:37](http://github.com/chi-middleware/proxy@v1.1.1/middleware.go:37) (0x1fa27b6)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea/routers/common/middleware.go:32 (0x1fa8592)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2084 (0x92390e)
        /home/build/build-deploy-gitea/build/gitea-bin/pkg/mod/[github.com/go-chi/chi/v5@v5.0.7/mux.go:88](http://github.com/go-chi/chi/v5@v5.0.7/mux.go:88) (0x1721961)
        /home/build/build-deploy-gitea/build/gitea/modules/web/route.go:200 (0x1f3a44d)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:2916 (0x926efa)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/net/http/server.go:1966 (0x9223b6)
        /home/build/build-deploy-gitea/build/go-1.18.8/go/src/runtime/asm_amd64.s:1571 (0x46d5c0)

ctx.Repo.GitRepo is nil, it should be checked but not in this git module.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Nov 27, 2022
@zeripath
Copy link
Contributor

It would still be really useful to figure out how ctx.Repo.GitRepo was nil here. I have a feeling that the there was an error higher up that didn't get handled properly. It would be really helpful to get the context for the logs i.e check if there were any other log lines for [636fd44c-60]

@zeripath
Copy link
Contributor

zeripath commented Nov 27, 2022

Aha! I have understood the problem:

// ReferencesGitRepo injects the GitRepo into the Context
// you can optional skip the IsEmpty check
func ReferencesGitRepo(allowEmpty ...bool) func(ctx *APIContext) (cancel context.CancelFunc) {
	return func(ctx *APIContext) (cancel context.CancelFunc) {
		// Empty repository does not have reference information.
		if ctx.Repo.Repository.IsEmpty && !(len(allowEmpty) != 0 && allowEmpty[0]) {
			return
		}
                ...

Here allowEmpty = []bool{} -> len(allowEmpty) == 0 -> !(len(allowEmpty) != 0 && allowEmpty[0]) == true

You are running the API GetBranch call on an "empty" repository.

Now, I have a feeling that this code should be handling the error condition itself but maybe I'm wrong and we want the API endpoints to have to check for empty repos themselves BUT I would have thought that if an API wanted to deal with empty repos it would indicate that by using allowEmpty = true.

Following this reasoning this PR is not really correct and instead we should be getting the GetBranch endpoint to check for empty OR make ReferencesGitRepo error out if there is an empty repo.

@zeripath
Copy link
Contributor

zeripath commented Dec 4, 2022

make lgtm work

@zeripath zeripath merged commit 4648584 into go-gitea:main Dec 4, 2022
zjjhot added a commit to zjjhot/gitea that referenced this pull request Dec 5, 2022
* giteaofficial/main:
  Ensure that Chinese punctuation is not ambiguous when locale is Chinese (go-gitea#22019)
  Use GhostUser if needed for TrackedTimes (go-gitea#22021)
  Add dumb-init to rootless docker (go-gitea#21775)
  On tag/branch-exist check, dont panic if repo is nil (go-gitea#21787)
  Fix ListBranches to handle empty case (go-gitea#21921)
  fix(web): reduce page jitter on browsers that support overlay scrollbar (go-gitea#21850)
  [skip ci] Updated licenses and gitignores
  Do not emit ambiguous character warning on rendered pages (go-gitea#22016)
lunny added a commit that referenced this pull request Dec 5, 2022
Backport #21787

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
@6543 6543 deleted the main_dont-panic branch December 23, 2022 22:48
@go-gitea go-gitea locked and limited conversation to collaborators May 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backport/done All backports for this PR have been created lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. outdated/backport/v1.18 This PR should be backported to Gitea 1.18 type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants