Skip to content
New issue

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

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

Already on GitHub? # to your account

[BUG]: 创建收藏遇到unique index 重复的问题 #366

Open
trim21 opened this issue Jun 16, 2023 · 11 comments
Open

[BUG]: 创建收藏遇到unique index 重复的问题 #366

trim21 opened this issue Jun 16, 2023 · 11 comments
Labels

Comments

@trim21
Copy link
Contributor

trim21 commented Jun 16, 2023

暂时还没没找到复现,但是有不少日志错误

对应的请求是用户 730066 用 post 请求想要收藏 335225

Error 1062 (23000): Duplicate entry '730066-335225' for key 'user_interest'
error stack:
github.com/bangumi/server/internal/collections/infra.mysqlRepo.updateOrCreateSubjectCollection
	github.com/bangumi/server/internal/collections/infra/mysql_repo.go:162
github.com/bangumi/server/internal/collections/infra.mysqlRepo.UpdateOrCreateSubjectCollection
	github.com/bangumi/server/internal/collections/infra/mysql_repo.go:117
github.com/bangumi/server/ctrl.Ctrl.UpdateSubjectCollection
	github.com/bangumi/server/ctrl/update_subject_collection.go:58
github.com/bangumi/server/web/handler/user.User.updateOrCreateSubjectCollection
	github.com/bangumi/server/web/handler/user/patch_subject_collection.go:93
github.com/bangumi/server/web/handler/user.User.PostSubjectCollection
	github.com/bangumi/server/web/handler/user/post_subject_collection.go:43
github.com/bangumi/server/web/mw.NeedLogin.func1
	github.com/bangumi/server/web/mw/middleware.go:32
github.com/bangumi/server/web/req.JSON.func1
	github.com/bangumi/server/web/req/content_type.go:29
github.com/bangumi/server/web/handler/common.Common.MiddlewareAccessTokenAuth.func1
	github.com/bangumi/server/web/handler/common/access_token.go:63
github.com/labstack/echo/v4.(*Echo).add.func1
	github.com/labstack/echo/v4@v4.10.2/echo.go:575
github.com/bangumi/server/web/mw/ua.DisableDefaultHTTPLibrary.func1
	github.com/bangumi/server/web/mw/ua/new.go:42
github.com/bangumi/server/web/mw/recovery.New.func1.1
	github.com/bangumi/server/web/mw/recovery/new.go:54
github.com/bangumi/server/web.New.func3.1
	github.com/bangumi/server/web/new.go:100
github.com/labstack/echo/v4/middleware.CORSWithConfig.func1.1
	github.com/labstack/echo/v4@v4.10.2/middleware/cors.go:199
github.com/bangumi/server/web.New.func2.1
	github.com/bangumi/server/web/new.go:68
github.com/labstack/echo/v4.(*Echo).ServeHTTP
	github.com/labstack/echo/v4@v4.10.2/echo.go:662
net/http.serverHandler.ServeHTTP
	net/http/server.go:2936

@RanKKI

@trim21 trim21 added the bug label Jun 16, 2023
@RanKKI
Copy link
Member

RanKKI commented Jun 19, 2023

可能是并发的问题?

代码似乎没有问题,
看看是不是同一请求发送两次?然后加锁

@trim21
Copy link
Contributor Author

trim21 commented Jun 19, 2023

看了一下请求日志,也不是同一个请求发了两次

@RanKKI
Copy link
Member

RanKKI commented Jun 24, 2023

加上 trace 之后这两天有无新的发现

@trim21
Copy link
Contributor Author

trim21 commented Jun 24, 2023

没有相同的报错了... 先close了

@trim21 trim21 closed this as completed Jun 24, 2023
@RanKKI
Copy link
Member

RanKKI commented Jun 24, 2023

真奇怪....

@trim21 trim21 reopened this Jul 11, 2023
@trim21
Copy link
Contributor Author

trim21 commented Jul 11, 2023

rror stack:
github.com/bangumi/server/internal/collections/infra.mysqlRepo.updateOrCreateSubjectCollection
	github.com/bangumi/server/internal/collections/infra/mysql_repo.go:156
github.com/bangumi/server/internal/collections/infra.mysqlRepo.UpdateOrCreateSubjectCollection
	github.com/bangumi/server/internal/collections/infra/mysql_repo.go:117
github.com/bangumi/server/ctrl.Ctrl.UpdateSubjectCollection
	github.com/bangumi/server/ctrl/update_subject_collection.go:58
github.com/bangumi/server/web/handler/user.User.updateOrCreateSubjectCollection
	github.com/bangumi/server/web/handler/user/patch_subject_collection.go:93
github.com/bangumi/server/web/handler/user.User.PostSubjectCollection
	github.com/bangumi/server/web/handler/user/post_subject_collection.go:43
github.com/bangumi/server/web/mw.NeedLogin.func1
	github.com/bangumi/server/web/mw/middleware.go:32
github.com/bangumi/server/web/req.JSON.func1
	github.com/bangumi/server/web/req/content_type.go:29

@trim21
Copy link
Contributor Author

trim21 commented Jul 11, 2023

err = errgo.Trace(T.WithContext(ctx).Create(obj))

这里

@trim21
Copy link
Contributor Author

trim21 commented Jul 11, 2023

看起来像是

s, err := r.getSubjectCollection(ctx, userID, subject.ID)
这里的问题,两个请求并发不安全

@RanKKI
Copy link
Member

RanKKI commented Jul 12, 2023

所以是两个请求一起进来么?

@trim21
Copy link
Contributor Author

trim21 commented Jul 12, 2023

是的,看日志应该是

@RanKKI
Copy link
Member

RanKKI commented Jul 12, 2023

加个锁吧,我晚点提个 pr

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants