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

Bad Access: Nil Dereference Case Occurs in log function of sugar.go #802

Closed
Osiris1975 opened this issue Mar 20, 2020 · 1 comment
Closed

Comments

@Osiris1975
Copy link

Hi,

I'm currently writing a CRUD microservice that uses zap logger. When I create an object via my POST endpoint, I have no problems. Here's my object model:

type Field struct {
	Id            int64     `json:"id" gorm:"primary_key"`
	PartnerId     int       `json:"partner_id" gorm:"not_null"`
	QualifiedName string    `json:"qualified_name" binding:"required" gorm:"unique; not_null"`
	Description   string    `json:"description"`
	DataType      DataType  `json:"data_type" binding:"required" validate:"oneof=0 1" enums:"string,int"`
	CreatedAt     time.Time `json:"created_at"`
	CreatedBy     int64     `json:"created_by"`
	UpdatedAt     time.Time `json:"updated_at"`
	UpdatedBy     int64     `json:"updated_by"`
}

As you can see from the tags, QualifiedName is required and unique. So if I try to recreate the same object, I should get an error indicating that an object with that same qualified name already exists.

Here's the code that handles creating the object:

func handleCreateField(c *gin.Context, svc field.RegistrationService) {
	user := auth.GetUser(c)

	var f field.Field
	var err error

	//FIXME: Currently trying to create an object that already exists leads to 'bad access: nil dereference'somewhere inside of a ShouldBondJSON goroutine
	if err = c.ShouldBindJSON(&f); err != nil {
		_ = c.Error(err)
		return
	}

	if user != nil {
		f.PartnerId = user.PartnerID
		f.CreatedBy = user.ID
		f.UpdatedBy = f.CreatedBy
	}

	if err = svc.Create(&f); err != nil {
		_ = c.Error(err)
		return
	}
	c.JSON(http.StatusOK, f)
}

If I step through the debugger, the error occurs somewhere in the call to c.ShouldBindJSON(&f). Eventually, stepping though leads to the "bad access: nil dereference" message triggered in this part of sugar.go.

func (s *SugaredLogger) log(lvl zapcore.Level, template string, fmtArgs []interface{}, context []interface{}) {
	// If logging at this level is completely disabled, skip the overhead of
	// string formatting.
	if lvl < DPanicLevel && !s.base.Core().Enabled(lvl) {
		return
	}

At this point, the request never returns a response, and continuing from the breakpoint I placed on if lvl < DPanicLevel && !s.base.Core().Enabled(lvl) just returns to this breakpoint, so it appears to be stuck in a goroutine or loop that won't terminate on its own. I would note that that value of s here is nil which is probably why I'm encountering this. What I can't figure out is why s is nil.

Any help or suggestions appreciated. Note that I'm also cross-posting on StackOverflow.

@prashantv
Copy link
Collaborator

Hi @Osiris1975, your issue has a combination of gin, gorm, and zap, and it's hard to tease apart what exactly is the issue. The code samples you provided have no reference to a logger or a log call, and so it's not clear how the log call even happens.

It's possible that your configuration to create the logger is incorrect (#777) or your logger is not created at all, so check those first.

If you continue to see this issue, please provide a minimal reproduction without any other libraries, so we can investigate the issue further.

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

No branches or pull requests

2 participants