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

Commit 9790794

Browse files
committed
localUpdate: move the DAG service Àdd call outside the lock
Since it's accessing a copy and not the original directory node.
1 parent 8d26210 commit 9790794

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

dir.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ func (d *Directory) updateChildEntry(c child) error {
9898
// new node in the DAG layer.
9999
func (d *Directory) localUpdate(c child) (*dag.ProtoNode, error) {
100100
d.lock.Lock()
101-
defer d.lock.Unlock()
102101

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

120-
err = d.dagService.Add(d.ctx, nd)
119+
nodeCopy := pbnd.Copy().(*dag.ProtoNode)
120+
// We copy the underlying node to add it to the DAG service and
121+
// propagate the update upwards (outside of the lock) because
122+
// other callers might operate on this directory node later
123+
// while *this* update is still in progress.
124+
125+
d.lock.Unlock()
126+
// NOTE: The original `nd` can't be accessed below this point.
127+
128+
err = d.dagService.Add(d.ctx, nodeCopy)
121129
if err != nil {
122130
return nil, err
123131
}
124132

125-
return pbnd.Copy().(*dag.ProtoNode), nil
126-
// TODO: Why do we need a copy?
133+
return nodeCopy, nil
127134
}
128135

129136
// Update child entry in the underlying UnixFS directory.

0 commit comments

Comments
 (0)