Comments (12)
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.
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.
@rwasef1830 Host is Linux?
from mime.
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.
Here's the commit where I fixed it: rwasef1830/grepripper@6d62c3c
from mime.
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.
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.
@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.
@rwasef1830 Okay, seems like reasonable.
But should I use static object? Or instance based lock is fine?
from mime.
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.
Done 7acf3f1
from mime.
👍
from mime.
Related Issues (20)
- Implement magic_list
- Building this project to net 4.5/x86 specification
- missing alpine linux x64 support HOT 1
- Unable to find libmagic-1 on Ubuntu 20.04 HOT 5
- linux-musl-x64 is missing from runtimeTargets HOT 4
- Libmagic not compatibile with Xamarin on mobile devices
- Different results using filePath vs stream input HOT 2
- Got error with visual studio 2015 HOT 3
- Update File to version 5.41 HOT 7
- libmagic dependency mismatch on MacOS Monterey (5.41) HOT 2
- "Could not find any valid magic files!" when included in a Nuget package HOT 5
- native lib for M1 Mac os HOT 15
- Update file to 5.44 HOT 5
- Does not pick up Parquet files HOT 2
- Copy native libs to correct build output directory for Azure Functions
- Different extension compared to the `file` command HOT 2
- Different results on Linux and Windows
- HeyRed.Mime.MagicException: Could not find any valid magic files! on Windows Server 2012 R2 when program is on path with ů
- 3.6.0 - libmagic-1.dll flagged as virus HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mime.