Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
localUpdate: move the DAG service Àdd call outside the lock
Browse files Browse the repository at this point in the history
Since it's accessing a copy and not the original directory node.
  • Loading branch information
schomatis committed Dec 22, 2018
1 parent 8d26210 commit 9790794
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions dir.go
Expand Up @@ -98,7 +98,6 @@ func (d *Directory) updateChildEntry(c child) error {
// new node in the DAG layer.
func (d *Directory) localUpdate(c child) (*dag.ProtoNode, error) {
d.lock.Lock()
defer d.lock.Unlock()

err := d.updateChild(c)
if err != nil {
Expand All @@ -117,13 +116,21 @@ func (d *Directory) localUpdate(c child) (*dag.ProtoNode, error) {
return nil, dag.ErrNotProtobuf
}

err = d.dagService.Add(d.ctx, nd)
nodeCopy := pbnd.Copy().(*dag.ProtoNode)
// We copy the underlying node to add it to the DAG service and
// propagate the update upwards (outside of the lock) because
// other callers might operate on this directory node later
// while *this* update is still in progress.

d.lock.Unlock()
// NOTE: The original `nd` can't be accessed below this point.

err = d.dagService.Add(d.ctx, nodeCopy)
if err != nil {
return nil, err
}

return pbnd.Copy().(*dag.ProtoNode), nil
// TODO: Why do we need a copy?
return nodeCopy, nil
}

// Update child entry in the underlying UnixFS directory.
Expand Down

0 comments on commit 9790794

Please sign in to comment.