From 2456cb65d878c6b60c258c60bc513453b88e3da7 Mon Sep 17 00:00:00 2001 From: zeripath Date: Tue, 15 Jan 2019 11:40:54 +0000 Subject: [PATCH] Ensure lfs check within transaction The previous code made it possible for a race condition to occur whereby a LFSMetaObject could be checked into the database twice. We should check if the LFSMetaObject is within the database and insert it if not in one transaction. --- models/lfs.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/models/lfs.go b/models/lfs.go index 711e5b0494453..39b0b2dd69a29 100644 --- a/models/lfs.go +++ b/models/lfs.go @@ -44,20 +44,20 @@ const ( func NewLFSMetaObject(m *LFSMetaObject) (*LFSMetaObject, error) { var err error - has, err := x.Get(m) + sess := x.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return nil, err + } + + has, err := sess.Get(m) if err != nil { return nil, err } if has { m.Existing = true - return m, nil - } - - sess := x.NewSession() - defer sess.Close() - if err = sess.Begin(); err != nil { - return nil, err + return m, sess.Commit() } if _, err = sess.Insert(m); err != nil {