1

Just to clear up any confusion about what the code does, it takes an 'Object' class (a class of float and numbers), writes to a file (using binary formatting), and uses an IPFS.Net implementation to write the file to the network. Creating the file is OK, but when saving the file the code hangs indefinitely in Unity. The code is below:

public static class IPFS_IO
{
    public static void AddObject(Object obs)
    {
        string path = Application.persistentDataPath + "/data.bytes";
        if (File.Exists(path))
        {
            File.Delete(path);
        }

        FileStream stream = File.Create(path);
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(stream, obs);
        stream.Close();

        Task<Ipfs.IFileSystemNode> taskA = AddObjectSaveHash(path);
        taskA.Wait();

        //string hashpath = Application.persistentDataPath + "/hash.txt";

        //StreamWriter writer = new StreamWriter(hashpath);
        //writer.Write(hash);
        //writer.Close();
    }

    async static Task<Ipfs.IFileSystemNode> AddObjectSaveHash(string path)
    {
        Batteries.Init();

        using (var ie = new IpfsEngine("".ToCharArray()))
        {
            return await ie.FileSystem.AddFileAsync(path)
                           .ConfigureAwait(false);
        }
    }

Is this a common deadlock, or is it just too much data fed into the network? Bearing in mind:

a) This is a C# implementation to be used in the Unity Game Engine

b) I am using Richard Schneiders implementation (github: https://github.com/richardschneider/net-ipfs-engine)

Thanks for any help or problems you guys can spot.

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • `Is this a common deadlock` - [most likely](https://stackoverflow.com/q/15021304/11683). Have you tried `await AddObjectSaveHash(path)` from `AddObject`? – GSerg May 05 '18 at 11:53
  • And make the function `async void`. – piojo May 05 '18 at 11:54
  • This is great. Sorry, I'm a little new to async operations. Thanks for the help! – Samuel Lopes May 05 '18 at 12:01
  • 3
    The reason it deadlocks even though you are using `.ConfigureAwait(false)` has to be that ["Using `ConfigureAwait(false)` to avoid deadlocks is a dangerous practice. You would have to use `ConfigureAwait(false)` for every await in the transitive closure of all methods called by the blocking code, including all third- and second-party code."](http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html), and your library [does not use `ConfigureAwait(false)`](https://github.com/richardschneider/net-ipfs-engine/blob/46ef4773b5e1e1a14ca0681a47f925be14f3b986/src/CoreApi/FileSystemApi.cs). – GSerg May 05 '18 at 12:02
  • Use [coroutines](https://docs.unity3d.com/Manual/Coroutines.html). – Draco18s no longer trusts SE May 05 '18 at 18:39

0 Answers0