Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix crash on dispose while running 'RunAsync' #369

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 49 additions & 17 deletions Packages/com.github.asus4.tflite.common/Runtime/BaseVisionTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public abstract class BaseVisionTask : IDisposable
protected int channels;
protected TextureToNativeTensor textureToTensor;

private readonly SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
private bool isDisposed = false;

public AspectMode AspectMode { get; set; } = AspectMode.None;

Expand Down Expand Up @@ -57,8 +59,20 @@ public virtual void Load(byte[] model, InterpreterOptions options)

public virtual void Dispose()
{
interpreter?.Dispose();
textureToTensor?.Dispose();
semaphore.Wait();
try
{
if (!isDisposed)
{
interpreter?.Dispose();
textureToTensor?.Dispose();
isDisposed = true;
}
}
finally
{
semaphore.Release();
}
}

/// <summary>
Expand All @@ -67,6 +81,11 @@ public virtual void Dispose()
/// <param name="texture">A texture for model input</param>
public virtual void Run(Texture texture)
{
if (isDisposed)
{
throw new ObjectDisposedException(nameof(BaseVisionTask));
}

// Pre process
preprocessPerfMarker.Begin();
PreProcess(texture);
Expand Down Expand Up @@ -100,24 +119,37 @@ protected virtual void PreProcess(Texture texture)
protected abstract void PostProcess();

// Only available when UniTask is installed
#if TFLITE_UNITASK_ENABLED
#if TFLITE_UNITASK_ENABLED || true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, this debug flag should be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry!

public virtual async UniTask RunAsync(Texture texture, CancellationToken cancellationToken)
{
// Pre process
preprocessPerfMarker.Begin();
await PreProcessAsync(texture, cancellationToken);
preprocessPerfMarker.End();

// Run inference in BG thread
await UniTask.SwitchToThreadPool();
runPerfMarker.Begin();
interpreter.Invoke();
runPerfMarker.End();
if (isDisposed)
{
throw new ObjectDisposedException(nameof(BaseVisionTask));
}

// Post process
postprocessPerfMarker.Begin();
await PostProcessAsync(cancellationToken);
postprocessPerfMarker.End();
await semaphore.WaitAsync(cancellationToken);
try
{
// Pre process
preprocessPerfMarker.Begin();
await PreProcessAsync(texture, cancellationToken);
preprocessPerfMarker.End();

// Run inference in BG thread
await UniTask.SwitchToThreadPool();
runPerfMarker.Begin();
interpreter.Invoke();
runPerfMarker.End();

// Post process
postprocessPerfMarker.Begin();
await PostProcessAsync(cancellationToken);
postprocessPerfMarker.End();
}
finally
{
semaphore.Release();
}

// Back to main thread
await UniTask.SwitchToMainThread();
Expand Down