From 0fa59833378c03651a49e28f2398a56a9b9659b5 Mon Sep 17 00:00:00 2001 From: Dominic Della Valle Date: Fri, 25 Oct 2019 09:51:55 -0400 Subject: [PATCH] try not to leak --- plugin/plugins/filesystem/nodes/base.go | 8 -------- plugin/plugins/filesystem/nodes/keyfs.go | 6 ++++++ plugin/plugins/filesystem/nodes/pinfs.go | 6 ++++++ plugin/plugins/filesystem/nodes/root.go | 8 ++++++++ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/plugin/plugins/filesystem/nodes/base.go b/plugin/plugins/filesystem/nodes/base.go index f0880c99a12..4d801068ff4 100644 --- a/plugin/plugins/filesystem/nodes/base.go +++ b/plugin/plugins/filesystem/nodes/base.go @@ -144,15 +144,7 @@ func (ib *IPFSBase) close() error { } if ib.filesystemCancel != nil { - /* TODO: only do this on the last close to the root - if ib.proxy != nil { - if err := ib.proxy.Close(); err != nil { - ib.Logger.Error(err) - } - lastErr = err - } ib.filesystemCancel() - */ } if ib.operationsCancel != nil { diff --git a/plugin/plugins/filesystem/nodes/keyfs.go b/plugin/plugins/filesystem/nodes/keyfs.go index 77ca354528c..102f365ae63 100644 --- a/plugin/plugins/filesystem/nodes/keyfs.go +++ b/plugin/plugins/filesystem/nodes/keyfs.go @@ -2,6 +2,7 @@ package fsnodes import ( "context" + "runtime" "github.com/hugelgupf/p9/p9" "github.com/hugelgupf/p9/unimplfs" @@ -39,6 +40,11 @@ func KeyFSAttacher(ctx context.Context, core coreiface.CoreAPI, ops ...nodeopts. kd.proxy = subsystem.(fsutils.WalkRef) + // detach from our proxied system when we fall out of memory + runtime.SetFinalizer(kd, func(keyRoot *KeyFS) { + keyRoot.proxy.Close() + }) + return kd } diff --git a/plugin/plugins/filesystem/nodes/pinfs.go b/plugin/plugins/filesystem/nodes/pinfs.go index d5c2ec47b29..4ad3641249e 100644 --- a/plugin/plugins/filesystem/nodes/pinfs.go +++ b/plugin/plugins/filesystem/nodes/pinfs.go @@ -4,6 +4,7 @@ import ( "context" "fmt" gopath "path" + "runtime" "github.com/hugelgupf/p9/p9" "github.com/hugelgupf/p9/unimplfs" @@ -43,6 +44,11 @@ func PinFSAttacher(ctx context.Context, core coreiface.CoreAPI, ops ...nodeopts. pd.proxy = subsystem.(fsutils.WalkRef) + // detach from our proxied system when we fall out of memory + runtime.SetFinalizer(pd, func(pinRoot *PinFS) { + pinRoot.proxy.Close() + }) + return pd } diff --git a/plugin/plugins/filesystem/nodes/root.go b/plugin/plugins/filesystem/nodes/root.go index 017ca274024..4a21b63229b 100644 --- a/plugin/plugins/filesystem/nodes/root.go +++ b/plugin/plugins/filesystem/nodes/root.go @@ -2,6 +2,7 @@ package fsnodes import ( "context" + "runtime" "github.com/hugelgupf/p9/p9" "github.com/hugelgupf/p9/unimplfs" @@ -116,6 +117,13 @@ func RootAttacher(ctx context.Context, core coreiface.CoreAPI, ops ...nodeopts.A } } + // detach from our proxied systems when we fall out of memory + runtime.SetFinalizer(ri, func(root *RootIndex) { + for _, ss := range ri.subsystems { + ss.file.Close() + } + }) + return ri }