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

Don't panic when in detached head state. #5943

Merged
merged 16 commits into from
May 18, 2023
Merged

Conversation

nicktobey
Copy link
Contributor

Fixes #5871

Many commands currently panic when in a detached head state, such as commit (#5744), checkout (#5839), and many others. This turns those panics into errors.

@nicktobey nicktobey requested a review from zachmu May 12, 2023 18:34
Copy link
Member

@zachmu zachmu left a comment

Choose a reason for hiding this comment

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

LGTM, one major comment and a few nits

@@ -172,7 +175,11 @@ func printBranches(ctx context.Context, dEnv *env.DoltEnv, apr *argparser.ArgPar
}

if verbose {
cm, err := dEnv.DoltDB.Resolve(ctx, cs, dEnv.RepoStateReader().CWBHeadRef())
headRef, err := dEnv.RepoStateReader().CWBHeadRef()
Copy link
Member

Choose a reason for hiding this comment

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

You can re-use currentBranch from above here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -195,7 +202,11 @@ func checkoutNewBranch(ctx context.Context, dEnv *env.DoltEnv, apr *argparser.Ar
if !remoteOk {
return nil
}
verr = SetRemoteUpstreamForBranchRef(dEnv, remoteName, remoteBranchName, dEnv.RepoStateReader().CWBHeadRef())
headRef, err := dEnv.RepoStateReader().CWBHeadRef()
Copy link
Member

Choose a reason for hiding this comment

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

You can re-use the headRef from above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

remoteName = defRemote.Name
currentBranch, err := dEnv.RepoStateReader().CWBHeadRef()
if err != nil {
verr = errhand.BuildDError("fatal: The current branch could not be identified").Build()
Copy link
Member

Choose a reason for hiding this comment

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

You probably want the err in here as a cause

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -654,6 +661,9 @@ func (db Database) GetWorkingSet(ctx *sql.Context) (*doltdb.WorkingSet, error) {
if !ok {
return nil, fmt.Errorf("no root value found in session")
}
if dbState.WorkingSet == nil {
Copy link
Member

Choose a reason for hiding this comment

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

Don't know if we actually want this behavior. In general an accessor like this shouldn't second-guess what a client is going to do with the things it returns. It's a valid state for a session to not have a working set, and returning a nil value here is fine, it doesn't need to be an error. Generally speaking we don't want the presence of an error to signal needed changes in business logic, only actual errors that mean something went wrong, which isn't the case here.

workingSet, err := s.session.WorkingSet(sql.NewContext(context.Background()), s.dbName)
if err != nil {
// TODO: fix this interface
panic(err)
return nil, err
Copy link
Member

Choose a reason for hiding this comment

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

This is where the error should originate, not session.WorkingSet

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In principle, I agree with your comments. It should be up to the client to decide whether the lack of a working set is valid, and up the api to return results in a form that require clients to consider this.

The trouble is that a lot of places call session.Workingset without calling CWBHeadRef, and don't do any checking on the result, and adding checks to all of them would complicate all those commands while doing nothing to prevent this same mistake from happening if we add another command that calls session.Workingset.

The long term solution is to change the interface to one where instead of clients requesting the working set, they request what they want the working set for, and the and session returns the best result. But until we have that, the priority is to prevent panics and make it harder to introduce panics in the future. This does that.

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with this for now, please add a TODO to this effect in the method doc for Database.GetWorkingSet()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@timsehn
Copy link
Contributor

timsehn commented May 18, 2023

Where we at with this one?

@nicktobey nicktobey merged commit a4a97d2 into main May 18, 2023
@nicktobey nicktobey deleted the nicktobey/detached_head branch May 18, 2023 23:44
@github-actions
Copy link

@coffeegoddd DOLT

name add_cnt delete_cnt update_cnt latency
adds_only 60000 0 0 0.9
adds_updates_deletes 60000 60000 60000 5.18
deletes_only 0 60000 0 2.47
updates_only 0 0 60000 3.3

@github-actions
Copy link

@coffeegoddd DOLT

name add_cnt delete_cnt update_cnt latency
adds_only 60000 0 0 0.82
adds_updates_deletes 60000 60000 60000 4.86
deletes_only 0 60000 0 2.37
updates_only 0 0 60000 3.11

@github-actions
Copy link

@coffeegoddd DOLT

name add_cnt delete_cnt update_cnt latency
adds_only 60000 0 0 0.84
adds_updates_deletes 60000 60000 60000 4.98
deletes_only 0 60000 0 2.37
updates_only 0 0 60000 3.18

@github-actions
Copy link

@coffeegoddd DOLT

name add_cnt delete_cnt update_cnt latency
adds_only 60000 0 0 0.83
adds_updates_deletes 60000 60000 60000 4.9
deletes_only 0 60000 0 2.35
updates_only 0 0 60000 3.21

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

Successfully merging this pull request may close these issues.

Many commands fail in detached head by trying to access nil pointer Working Set
3 participants