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

Infinite loop and crash in self-associations #4108

Closed
inliquid opened this issue Feb 19, 2021 · 6 comments
Closed

Infinite loop and crash in self-associations #4108

inliquid opened this issue Feb 19, 2021 · 6 comments
Assignees
Labels

Comments

@inliquid
Copy link

GORM Playground Link

This is not possible to represent via gorm playground.

Description

Imagine I want to add a friends of user via self assiciation:

type User struct {
	ID      uint
	Name    string
	Friends []*User `gorm:"many2many:friends;association_jointable_foreignkey:friend_id"`
}

When I'm adding a friend to a user, it works. But when I'm trying to add a user as a friend of friend, so in reverse, this call falls into infinite loop.

EXAMPLE

package main

import (
	"context"
	"flag"
	"time"

	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

var (
	db    *gorm.DB
	newDB = flag.Bool("newDB", false, "Set this flag to create new DB.")
)

func init() {
	flag.Parse()

	var err error
	db, err = gorm.Open(postgres.Open("postgres://gorm:[email protected]/gorm?sslmode=disable"), &gorm.Config{
		Logger:                 logger.Default.LogMode(logger.Info),
		SkipDefaultTransaction: true,
	})
	panicErr(err)

	if *newDB {
		err = db.Migrator().DropTable(
			"friends",
			&User{},
		)
		panicErr(err)
	}

	err = db.Migrator().AutoMigrate(&User{})
	panicErr(err)
}

type User struct {
	ID      uint
	Name    string
	Friends []*User `gorm:"many2many:friends;association_jointable_foreignkey:friend_id"`
}

func main() {
	u := &User{Name: "User"}
	f := &User{Name: "Friend"}

	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
	defer cancel()

	// Add users
	err := db.WithContext(ctx).Create(u).Error
	panicErr(err)
	err = db.WithContext(ctx).Create(f).Error
	panicErr(err)

	// Add friends

	// THIS ONE WORKS FINE
	err = db.WithContext(ctx).
		Model(u).
		Association("Friends").
		Append(f)
	panicErr(err)

	// THIS ONE BREAKS INTO INFINITE LOOP
	err = db.WithContext(ctx).
		Model(f).
		Association("Friends").
		Append(u)
	panicErr(err)

}

func panicErr(err error) {
	if err != nil {
		panic(err)
	}
}
@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Feb 19, 2021
@github-actions
Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@inliquid
Copy link
Author

I would add this to playground but app is crashing and should be stopped forcefully, so it's not possible to just put a code into some test.

@github-actions
Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@inliquid
Copy link
Author

WTF

@inliquid
Copy link
Author

@jinzhu this is very serious problem, please reopen.

And, personally, I don't think that forcefully closing absolutely all bugs (when "bug" was selected by reporter!!!) in 2 days when people don't have enough time, efforts or skills to do all this fancy magic passes is misleading, and will make quality of library much less than it could be. This is disrespectful to reporters.

@jinzhu
Copy link
Member

jinzhu commented Feb 23, 2021

Fixed, thank you for your report.


Sorry, rarely check those "type:missing reproduction steps" issues due to limited time, most of those kind issues usually are misusage, so the GitHub action really helped me to focus on high ROI issues.

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

No branches or pull requests

2 participants