diff --git a/commonspace/commongetter.go b/commonspace/commongetter.go index 12abdf4c..6807265b 100644 --- a/commonspace/commongetter.go +++ b/commonspace/commongetter.go @@ -5,18 +5,21 @@ import ( "github.com/anytypeio/any-sync/commonspace/object/syncobjectgetter" "github.com/anytypeio/any-sync/commonspace/object/tree/objecttree" "github.com/anytypeio/any-sync/commonspace/object/treegetter" + "sync/atomic" ) type commonGetter struct { treegetter.TreeGetter spaceId string reservedObjects []syncobjectgetter.SyncObject + spaceIsClosed *atomic.Bool } -func newCommonGetter(spaceId string, getter treegetter.TreeGetter) *commonGetter { +func newCommonGetter(spaceId string, getter treegetter.TreeGetter, spaceIsClosed *atomic.Bool) *commonGetter { return &commonGetter{ - TreeGetter: getter, - spaceId: spaceId, + TreeGetter: getter, + spaceId: spaceId, + spaceIsClosed: spaceIsClosed, } } @@ -25,6 +28,9 @@ func (c *commonGetter) AddObject(object syncobjectgetter.SyncObject) { } func (c *commonGetter) GetTree(ctx context.Context, spaceId, treeId string) (objecttree.ObjectTree, error) { + if c.spaceIsClosed.Load() { + return nil, ErrSpaceClosed + } if obj := c.getReservedObject(treeId); obj != nil { return obj.(objecttree.ObjectTree), nil } @@ -41,6 +47,9 @@ func (c *commonGetter) getReservedObject(id string) syncobjectgetter.SyncObject } func (c *commonGetter) GetObject(ctx context.Context, objectId string) (obj syncobjectgetter.SyncObject, err error) { + if c.spaceIsClosed.Load() { + return nil, ErrSpaceClosed + } if obj := c.getReservedObject(objectId); obj != nil { return obj, nil } diff --git a/commonspace/space.go b/commonspace/space.go index 4cb02e9f..b29cd513 100644 --- a/commonspace/space.go +++ b/commonspace/space.go @@ -127,7 +127,7 @@ type space struct { handleQueue multiqueue.MultiQueue[HandleMessage] - isClosed atomic.Bool + isClosed *atomic.Bool treesUsed atomic.Int32 } diff --git a/commonspace/spaceservice.go b/commonspace/spaceservice.go index c26839b8..7ae37a81 100644 --- a/commonspace/spaceservice.go +++ b/commonspace/spaceservice.go @@ -17,6 +17,7 @@ import ( "github.com/anytypeio/any-sync/net/peer" "github.com/anytypeio/any-sync/net/pool" "github.com/anytypeio/any-sync/nodeconf" + "sync/atomic" ) const CName = "common.commonspace" @@ -116,7 +117,8 @@ func (s *spaceService) NewSpace(ctx context.Context, id string) (Space, error) { } lastConfiguration := s.configurationService.GetLast() - getter := newCommonGetter(st.Id(), s.treeGetter) + var spaceIsClosed = &atomic.Bool{} + getter := newCommonGetter(st.Id(), s.treeGetter, spaceIsClosed) syncStatus := syncstatus.NewNoOpSyncStatus() // this will work only for clients, not the best solution, but... if !lastConfiguration.IsResponsible(st.Id()) { @@ -141,6 +143,7 @@ func (s *spaceService) NewSpace(ctx context.Context, id string) (Space, error) { configuration: lastConfiguration, peerManager: peerManager, storage: st, + isClosed: spaceIsClosed, } return sp, nil }