You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"Cannot access a disposed object." with multithreading #912
I have an issue with Tensorflow.NET 0.70.1 and a multithreading console application:
I have the same problem with CPU or GPU native library, tensorflow 2.7.0.
Here you can find a sample project: TensorflowMultithreadingTest.zip
Note: I cannot use using for the inference because I need to reuse the same session, I don't think is feasible to create a new Session every time.
Note 2: Using modelSession.Dispose() at the end of the processing or using GC.SuppressFinalize(modelSession) after the declaration solve the problem, but I don't think it's the best solution.
I have this problem only on .NET Framework 4.8, not on .NET 5 or .NET 6 for some reason. I read online that maybe there are some difference in the finalize phase between .NET Framework and .NET Core: https://stackoverflow.com/a/44735403
As a workaround, in my project I created a new Session in a wrapper class, in this class I disabled the finalizer with GC.SuppressFinalize(_session) and I manually deleted the session in the finalizer of the wrapper using a new Status object and not the static one: ~Session() => c_api.TF_DeleteSession(_session, _status.Handle)
@Superpiffer Is it possible to PR a patch with compile condition?
I don't know exactly how to solve the problem. My understanding is that the DisposeUnmanagedResources method always use the static tf.Status instance to delete the session. For some reason, with .NET 4.8, at the last thread tf.Status is already disposed, causing the exception:
My workaround simply suppress the finalizer of the Session instance, implementing a new finalizer without tf.Status.
Anyway a stupid solution like this one works no problem, but I don't know if can have other issues elsewhere: c_api.TF_DeleteSession(handle, c_api.TF_NewStatus());
Activity
Superpiffer commentedon May 3, 2022
@Oceania2018 I have an update on this issue:
GC.SuppressFinalize(_session)and I manually deleted the session in the finalizer of the wrapper using a new Status object and not the static one:~Session() => c_api.TF_DeleteSession(_session, _status.Handle)Wrapper class:
I don't know if this make sense, but apparently it works for now...
Oceania2018 commentedon Jan 31, 2023
@Superpiffer Is it possible to PR a patch with compile condition?
Superpiffer commentedon Feb 1, 2023
I don't know exactly how to solve the problem. My understanding is that the DisposeUnmanagedResources method always use the static tf.Status instance to delete the session. For some reason, with .NET 4.8, at the last thread tf.Status is already disposed, causing the exception:
TensorFlow.NET/src/TensorFlowNET.Core/Sessions/BaseSession.cs
Lines 291 to 295 in c5cdf2c
My workaround simply suppress the finalizer of the Session instance, implementing a new finalizer without tf.Status.
Anyway a stupid solution like this one works no problem, but I don't know if can have other issues elsewhere:
c_api.TF_DeleteSession(handle, c_api.TF_NewStatus());Superpiffer commentedon Feb 10, 2023
@Oceania2018 I can close the issue, thanks. a7c9a75