Giter VIP home page Giter VIP logo

Comments (12)

hey-red avatar hey-red commented on May 24, 2024

How to reproduce it?

I'm trying to create threads without pool like this:

using System;
using System.IO;
using System.Threading;

using HeyRed.Mime;

namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var files = Directory.GetFiles("/path/to/files");

            for (int i = 0; i < 500; i++)
            {
                Thread t = new Thread(() => 
                {
                    string filePath = files[i];

                    var magic = new Magic(MagicOpenFlags.MAGIC_MIME_TYPE);

                    string mimeType = magic.Read(filePath);

                    Console.WriteLine($"File: {filePath}: {mimeType}");
                });

                t.Start();
            }
        }
    }
}

But it works as expected.
Btw if we need lock inside constructor, then we need it around all methods that works with pointer.

from mime.

rwasef1830 avatar rwasef1830 commented on May 24, 2024

It happened to me in Parallel.ForEach on a big (1000s of items) array and creating a Magic instance inside the body lambda on quad-core machine.
It would randomly crash with Heap corruption when the instance got used and randomly during creation also.

from mime.

hey-red avatar hey-red commented on May 24, 2024

@rwasef1830 Host is Linux?

from mime.

rwasef1830 avatar rwasef1830 commented on May 24, 2024

No, I was running my app on Windows 10. Once I locked around Magic instantiation, I could use the multiple instances simultaneously without problem.

from mime.

rwasef1830 avatar rwasef1830 commented on May 24, 2024

Here's the commit where I fixed it: rwasef1830/grepripper@6d62c3c

from mime.

hey-red avatar hey-red commented on May 24, 2024

Hmm, when I'm share single instance of Magic I got same issue, but if I create new instance in parallel loop it's work as expected.
So yes, it's covered in remarks
I think we should leave it as it is, because Magic is low level abstraction and most users not needed to go deeper than MimeGuesser.
But I'm should hightlight it in readme anyway.
Thanks for report 👍

from mime.

hey-red avatar hey-red commented on May 24, 2024

Also, I'm see golang implementation and its uses mutex for all operations
https://github.com/rakyll/magicmime/blob/master/magicmime.go#L181
🤔

from mime.

rwasef1830 avatar rwasef1830 commented on May 24, 2024

@hey-red the problem is that magic_open called in the constructor is not safe, so even the constructor needs to be locked. You can add a lock inside around magic_open, because libmagic changes shared memory on library initialization.

People can realize that magic instances aren't thread safe, but they don't realize that even Magic creation is also not thread safe.

from mime.

hey-red avatar hey-red commented on May 24, 2024

@rwasef1830 Okay, seems like reasonable.
But should I use static object? Or instance based lock is fine?

from mime.

rwasef1830 avatar rwasef1830 commented on May 24, 2024

Must be private static readonly locking object. @hey-red Used ONLY for construction.
Don't put any locks in other methods. Once you make different Magic, they can all work in parallel without problem

from mime.

hey-red avatar hey-red commented on May 24, 2024

Done 7acf3f1

from mime.

rwasef1830 avatar rwasef1830 commented on May 24, 2024

👍

from mime.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.