Skip to content

Fix sequence hold handle - #256

Merged
nuskey8 merged 2 commits into
annulusgames:mainfrom
amenonegames:bugfix-fix-sequence-hold-handle
Dec 20, 2025
Merged

Fix sequence hold handle#256
nuskey8 merged 2 commits into
annulusgames:mainfrom
amenonegames:bugfix-fix-sequence-hold-handle

Conversation

@amenonegames

@amenonegames amenonegames commented Dec 18, 2025

Copy link
Copy Markdown
Contributor

Issue1 : early return

The OnComplete method of the Sequence contained an early return statement: if (!handle.IsActive()) return;. However, since OnComplete is always called after the State transitions to Complete, the early return clause was always executed.

Issue2 : Preserved handle

Additionally, when adding a handle to the sequence, the Preserve flag was automatically set.
As a result, handles remained in memory even after the sequence execution completed.

Resolution1 : early return

I added an IsValid method that checks only whether the handle's version is appropriate and whether the handle exists in storage, without referencing the state.
The early return clause in Sequence.OnComplete was replaced with this IsValid check.

Resolution2 : Preserved handle

Additionally, upon the sequence's overall completion, we modified the behavior to cancel all handles within the sequence and discard preserved handles.

Verification

Verified execution using a combination of Join/Append/AppendInterval.
The debug screen results at the time of Motion completion are as follows.

before after
image image

※ I tested it in following repository.
https://github.com/amenonegames/LitMotionSequenceTest

Related Issue

#209

@amenonegames
amenonegames marked this pull request as draft December 18, 2025 11:29
@amenonegames

amenonegames commented Dec 18, 2025

Copy link
Copy Markdown
Contributor Author

The Join operation seems to work fine, but an error occurred during the Append operation. I will conduct a follow-up investigation.
Fixed this.

@amenonegames
amenonegames marked this pull request as ready for review December 19, 2025 15:26
@amenonegames
amenonegames force-pushed the bugfix-fix-sequence-hold-handle branch from e0ec4f4 to a2b2bb9 Compare December 19, 2025 15:43
@vietdungdev

Copy link
Copy Markdown

I will merge it into my fork <3

@nuskey8
nuskey8 merged commit 55873cd into annulusgames:main Dec 20, 2025
@nuskey8

nuskey8 commented Dec 20, 2025

Copy link
Copy Markdown
Member

Merged the PR. Thank you!!!

@vietdungdev

Copy link
Copy Markdown

After careful consideration, I found that only one line of code modification is needed in the MotionSequenceSource.cs file.
Adding the check IsInvalid function is not necessary.

image

@amenonegames

amenonegames commented Dec 20, 2025

Copy link
Copy Markdown
Contributor Author

Thank you for vietdungdev's investigation!
Actually, I have considered the suggested fix before create pr. While vietdungdev may already be aware, I'll share the concerns I considered for reference.

The original if (!handle.IsActive()) in this section contained two function:

  • function check if is the Version correct and the Handle stored in Storage
  • function check if is the Handle's State either Running or Preserved

Therefore, I believe there are two possible interpretations for the original intent of placing if (!handle.IsActive()):

  1. Intended to verify the Version and Storage storage were appropriate before executing
  2. Intended to execute only if the State was not Running, but this was a typo

I chose interpretation 1 and opted to retain the IsValid check.
The reason is that if a method were executed on an invalid Handle, it could potentially cause an Exception.

This decision was simply based on my incomplete understanding of the library as a whole, opting for a safer approach that seemed to minimize review burden.
If vietdungdev's suggestion is appropriate, I would appreciate it if you could consider it !

@vietdungdev

Copy link
Copy Markdown

@amenonegames
I think it's a typo because it makes more sense if the result is reversed.
You can see the OnComplete call logic below:

In UpdateRunner.cs

if (status is MotionStatus.Completed && state.WasStatusChanged)
{
	managedData.InvokeOnComplete();
}

In MotionManager.cs

public bool IsActive(MotionHandle handle)
{
	ref var state = ref unmanagedDataArray[slot.DenseIndex].Core.State;
	return state.Status is MotionStatus.Scheduled or MotionStatus.Delayed or MotionStatus.Playing ||
										(state.Status is MotionStatus.Completed && state.IsPreserved);
}

This means that in the OnComplete function, if the handle is not IsPreserved, IsActive will always be false because state.Status is now MotionStatus.Completed.
If the handle is complete, there is no reason to prevent memory release.

By modifying the function as below, the new logic will be to check handle.IsActive() to determine if this handle is IsPreserved.
If it is not IsPreserved, it means IsActive == false, the handle is completed and the memory for the sequence and child handles in the item.Handle needs to be released.
If it is IsPreserved, then IsActive == true, meaning it needs to be kept for the user to run again later; the user will manually call Cancel or Complete.

void OnComplete()
{
	if (handle.IsActive()) return;
	foreach (var item in Items)
	{
		MotionManager.Cancel(item.Handle, false);
	} 

	Return(this); 
}

vietdungdev added a commit to vietdungdev/LitMotion that referenced this pull request Dec 20, 2025
The contributor who discovered the issue is amenonegames here: annulusgames#256
@vietdungdev

Copy link
Copy Markdown

@nuskey8
I see many useful pull requests for bug fixes, but you don't seem to have considered them.
However, this particular pull request was merged quite quickly.
Is there a special reason for that?

@vietdungdev

Copy link
Copy Markdown

If this commit is merged, it will automatically release the sequence's handles even if handle.Preserved() was called previously, which is clearly contrary to the original design.

@nuskey8 nuskey8 mentioned this pull request Jan 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants