Skip to content

setParent breaks the Segment encapsulation #420

Open
@xendo

Description

@xendo

setParent and setParentSegment methods are exposed as Susbegment's API, but they have undocumented side effects. To give you two examples:

  • when a new Subsegment is created it updates its parent's reference counter parentSegment.incrementReferenceCount(); when subsegment is ended it will decrement that counter: parentSegment.decrementReferenceCount() . If between a Subsegment start and end we call setParentSegment(), we will increment and decrement reference counters for different parent's which is always never what we really want.
  • when setParent is called it only updates the parent of the current subsegment but it doesn't update the subsegments of that parent. That means that the old parent will keep the subsegment and new parent will not get the subsegment

so the actual reparenting should look sth like this:

oldParent = subsegment.getParent();
subsegment.setParent(newParent);
subsegment.setParentId(newParent.getId());
oldParent.decrementReferenceCount();
oldParent.removeSubsegment(subsegment);
newParent.incrementReferenceCount();
newParent.addSubsegment(subsegment);

the problem with that code is that it's not atomic, which may lead to weird issues if, for example newParent reference counter goes to 0 at the same time we are doing reparenting.

We need to either update the setParent's documentation to accomodate for that, or make reparenting safe with regards to reference counting and subsegment lists.

This is related to the workaround for #419 that I'm working on.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions