tags:

views:

187

answers:

5

I'm using (and referencing) two 3rd party dlls (a.dll, and b.dll) in two of my C# applications. I'm getting a repeatable problem where both applications hang while making a call to a function in the 3rd party library.

I tried to make a copy of a.dll and b.dll (a2.dll, and b2.dll) and use that in the second application, but it turns out that a.dll references b.dll, anb b.dll references a.dll and this does not load properly.

I suspect that there is a deadlock but in the 3rd party library. I can't use locking to prevent this. Each application enforces locking to make sure that that app only has one thread accessing the library at a time, but I can't lock across both programs.

So, my question is how can I solve this problem?

Can I tell the OS (Windows XP) that I don't want the dll to be shared?

Thanks, Joe

A: 

What about creating another thread - which is responsible for accessing this dll...That way the whole app doesn't freeze

PSU_Kardi
I do that already. However, I'm left with a critical thread in each application frozen and both applications -- although not entirely frozen -- are useless.
Joe H
+4  A: 

You can restrict access so only a single program at a time by using a named mutex. Named mutexes are restrictable to an entire operating system, so can be used to prevent access from multiple processes.

See the Mutex class for details.

Reed Copsey
Would this have to be in the 3rd party dll somewhere, or in my code?
Joe H
You can put this in your code - it basically works like your thread syncrhonization, except that you can "lock" the resource system wide this way.
Reed Copsey
Is Mutex as fast as lock(lockObject)?
Joe H
No. Getting a named mutex is a much slower operation. However, slow is still better than a deadlock :)
Reed Copsey
I have various applications calling into this dll about 5000 times a second under peak loads... so the speed issue bothers me. Isn't there any way to tell the OS not to share the library?
Joe H
Not really. The library really has to be doing something pretty awful to be causing problems when used by multiple processes.... It's not a "standard" problem - in your case, the normal OS means of handling isolation (via processes) isn't working for some reason.
Reed Copsey
A: 

Are these distinct applications? In that case, you shouldn't have any issues with single threaded access, as each process will handle that correctly. Can you clarify why you think there is a deadlock.

Michael Donohue
They are distinct applications. When I do a load test on both applications simultaneously, then the thread in each app which accesses the 3rd party dll hangs on the same function call into the dll (I see this by simultaneously debugging both). When I kill one app, then the thread which had been hanging in the other app completes its function call into the 3rd party library.
Joe H
Perhaps there is a mis-handled Mutex in the 3rd party library.
Joe H
+1  A: 

1) One way

  • Create a server application, which exports/implements the 3rd-party API, and makes the API available to other/remote processes (e.g. by using dot net remoting, or a web service, to expose the API over the network).
  • Within the server application, implement its API by delegating to the third party DLLs
  • Start only one instance of the server application: there's therefore only one instance of the 3rd-party DLLs being used
  • Have your existing applications use the API exported by your service, instead of using local instances of the 3rd-party DLLs
  • Within your service, implement locks (which you can because it's now within a single process).

2) Another way: use a kind of lock that's visible accross more than one process (e.g. the Mutex class).

3) Ask the vendors of the third-party DLLs: are you supposed to be able to run only one instance of this DLL at a time? If you are allowed to run more than one instance, what must you do to avoid deadlocks across processes?

ChrisW
1) That sounds like lots of work :-(2) Maybe...3) It's open source semi-abandonware. They haven't had an update since 2005.
Joe H
1) Doesn't bode very well with what you said later, about calling it 5000 times per second.
ChrisW
+1  A: 

I once had a similar problem with a discontinued 3rd party product.

There I used a disassembler and a hex editor with an integrated assembler to fix the underlying bug, but that was more or less luck because the cause was something trivial which could be derived by looking at the disassembly.

Depending on the actual cause that might be an option for you.

DR