Skip to content

Commit

Permalink
Fix memory clearing in adder
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
  • Loading branch information
whyrusleeping committed Dec 20, 2017
1 parent 5ad9f4d commit 3bab073
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,10 @@ func (adder *Adder) addFile(file files.File) error {
if err != nil {
return err
}
if err := mr.Flush(); err != nil {
if err := mr.FlushMemFree(adder.ctx); err != nil {
return err
}

adder.liveNodes = 0
}
adder.liveNodes++
Expand Down
30 changes: 30 additions & 0 deletions mfs/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,36 @@ func (kr *Root) Flush() error {
return nil
}

// FlushMemFree flushes the root directory and then uncaches all of its links.
// This has the effect of clearing out potentially stale references and allows
// them to be garbage collected.
// CAUTION: Take care not to ever call this while holding a reference to any
// child directories. Those directories will be bad references and using them
// may have unintended racy side effects.
// A better implemented mfs system (one that does smarter internal caching and
// refcounting) shouldnt need this method.
func (kr *Root) FlushMemFree(ctx context.Context) error {
dir, ok := kr.GetValue().(*Directory)
if !ok {
return fmt.Errorf("invalid mfs structure, root should be a directory")
}

if err := dir.Flush(); err != nil {
return err
}

names, err := dir.ListNames(ctx)
if err != nil {
return err
}

for _, name := range names {
dir.Uncache(name)
}

return nil
}

// closeChild implements the childCloser interface, and signals to the publisher that
// there are changes ready to be published
func (kr *Root) closeChild(name string, nd node.Node, sync bool) error {
Expand Down

0 comments on commit 3bab073

Please sign in to comment.