Skip to content

"Cannot access a disposed object." with multithreading  #912

Closed

Description

@Superpiffer

I have an issue with Tensorflow.NET 0.70.1 and a multithreading console application:
image
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.

Activity

  1. Superpiffer commented on May 3, 2022

    @Superpiffer
    ContributorAuthor

    @Oceania2018 I have an update on this issue:

    • 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)

    Wrapper class:

    internal class Session
    {
        private readonly Tensorflow.Session _session;
        private readonly Status _status = new Status();
        public Graph graph => _session.graph;
    
        public Session(Graph graph, ConfigProto config)
        {
            _session = new Tensorflow.Session(graph, config, _status);
            GC.SuppressFinalize(_session);
        }
    
        public NDArray run(Tensor tensor, FeedItem item) => _session.run(tensor, item);
        public NDArray[] run(Tensor[] tensors, FeedItem item) => _session.run(tensors, item);
    
        ~Session() => c_api.TF_DeleteSession(_session, _status.Handle);
    }

    I don't know if this make sense, but apparently it works for now...

  2. Oceania2018 commented on Jan 31, 2023

    @Oceania2018
    Member

    @Superpiffer Is it possible to PR a patch with compile condition?

  3. Superpiffer commented on Feb 1, 2023

    @Superpiffer
    ContributorAuthor

    @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:

    protected override void DisposeUnmanagedResources(IntPtr handle)
    {
    // c_api.TF_CloseSession(handle, tf.Status.Handle);
    c_api.TF_DeleteSession(handle, tf.Status.Handle);
    }

    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());

  4. Superpiffer commented on Feb 10, 2023

    @Superpiffer
    ContributorAuthor

    @Oceania2018 I can close the issue, thanks. a7c9a75

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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