Prevent multiple identical processes from spawning [duplicate] - c#

I have the following code:
using (Mutex mut = new Mutex(false, MUTEX_NAME))
{
if (mut.WaitOne(new TimeSpan(0, 0, 30)))
{
// Some code that deals with a specific TCP port
// Don't want this to run at the same time in another process
}
}
I've set a breakpoint within the if block, and ran the same code within another instance of Visual Studio. As expected, the .WaitOne call blocks. However, to my surprise, as soon as I continue in the first instance and the using block terminates, I get an exception in the second process about an abandoned Mutex.
The fix is to call ReleaseMutex:
using (Mutex mut = new Mutex(false, MUTEX_NAME))
{
if (mut.WaitOne(new TimeSpan(0, 0, 30)))
{
// Some code that deals with a specific TCP port
// Don't want this to run twice in multiple processes
}
mut.ReleaseMutex();
}
Now, things work as expected.
My Question: Usually the point of an IDisposable is it cleans up whatever state you put things in. I could see perhaps having multiple waits and releases within a using block, but when the handle to the Mutex is disposed, shouldn't it get released automatically? In other words, why do I need to call ReleaseMutex if I'm in a using block?
I'm also now concerned that if the code within the if block crashes, I'll have abandoned mutexes lying around.
Is there any benefit to putting Mutex in a using block? Or, should I just new up a Mutex instance, wrap it in a try/catch, and call ReleaseMutex() within the finally block (Basically implementing exactly what I thought Dispose() would do)

The documentation explains (in the "Remarks" section) that there is a conceptual difference between instantiating a Mutex object (which does not, in fact, do anything special as far as synchronization goes) and acquiring a Mutex (using WaitOne). Note that:
WaitOne returns a boolean, meaning that acquiring a Mutex can fail (timeout) and both cases must be handled
When WaitOne returns true, then the calling thread has acquired the Mutex and must call ReleaseMutex, or else the Mutex will become abandoned
When it returns false, then the calling thread must not call ReleaseMutex
So, there's more to Mutexes than instantiation. As for whether you should use using anyway, let's take a look at what Dispose does (as inherited from WaitHandle):
protected virtual void Dispose(bool explicitDisposing)
{
if (this.safeWaitHandle != null)
{
this.safeWaitHandle.Close();
}
}
As we can see, the Mutex is not released, but there is some cleanup involved, so sticking with using would be a good approach.
As to how you should proceed, you can of course use a try/finally block to make sure that, if the Mutex is acquired, that it gets properly released. This is likely the most straightforward approach.
If you really don't care about the case where the Mutex fails to be acquired (which you haven't indicated, since you pass a TimeSpan to WaitOne), you could wrap Mutex in your own class that implements IDisposable, acquire the Mutex in the constructor (using WaitOne() with no arguments), and release it inside Dispose. Although, I probably wouldn't recommend this, as this would cause your threads to wait indefinitely if something goes wrong, and regardless there are good reasons for explicitly handling both cases when attempting an acquire, as mentioned by #HansPassant.

This design decision was made a long, long time ago. Over 21 years ago, well before .NET was ever envisioned or the semantics of IDisposable were ever considered. The .NET Mutex class is a wrapper class for the underlying operating system support for mutexes. The constructor pinvokes CreateMutex, the WaitOne() method pinvokes WaitForSingleObject().
Note the WAIT_ABANDONED return value of WaitForSingleObject(), that's the one that generates the exception.
The Windows designers put the rock-hard rule in place that a thread that owns the mutex must call ReleaseMutex() before it exits. And if it doesn't that this is a very strong indication that the thread terminated in an unexpected way, typically through an exception. Which implies that synchronization is lost, a very serious threading bug. Compare to Thread.Abort(), a very dangerous way to terminate a thread in .NET for the same reason.
The .NET designers did not in any way alter this behavior. Not in the least because there isn't any way to test the state of the mutex other than by performing a wait. You must call ReleaseMutex(). And do note that your second snippet is not correct either; you cannot call it on a mutex that you didn't acquire. It must be moved inside of the if() statement body.

Ok, posting an answer to my own question. From what I can tell, this is the ideal way to implement a Mutex that:
Always gets Disposed
Gets Released iff WaitOne was successful.
Will not get abandoned if any code throws an exception.
Hopefully this helps someone out!
using (Mutex mut = new Mutex(false, MUTEX_NAME))
{
if (mut.WaitOne(new TimeSpan(0, 0, 30)))
{
try
{
// Some code that deals with a specific TCP port
// Don't want this to run twice in multiple processes
}
catch(Exception)
{
// Handle exceptions and clean up state
}
finally
{
mut.ReleaseMutex();
}
}
}
Update: Some may argue that if the code within the try block puts your resource in an unstable state, you should not release the Mutex and instead let it get abandoned. In other words, just call mut.ReleaseMutex(); when the code finishes successfully, and not put it within the finally block. The code acquiring the Mutex could then catch this exception and do the right thing.
In my situation, I'm not really changing any state. I'm temporarily using a TCP port and can't have another instance of the program run at the same time. For this reason, I think my solution above is fine but yours may be different.

One of the primary uses of a mutex is to ensure that the only code which will ever see a shared object in a state which doesn't satisfy its invariants is the code which (hopefully temporarily) put the object into that state. A normal pattern for code which needs to modify an object is:
Acquire mutex
Make changes to object which cause its state to become invalid
Make changes to object which cause its state to become valid again
Release mutex
If something goes wrong in after #2 has begun and before #3 has finished, the object may be left in a state which does not satisfy its invariants. Since the proper pattern is to release a mutex before disposing it, the fact that code disposes a mutex without releasing it implies that something went wrong somewhere. As such, it may not be safe for code to enter the mutex (since it hasn't been released), but there's no reason to wait for the mutex to be released (since--having been disposed--it never will be). Thus, the proper course of action is to throw an exception.
A pattern which is somewhat nicer than the one implemented by the .NET mutex object is to have the "acquire" method return an IDisposable object which encapsulates not the mutex, but rather a particular acquisition thereof. Disposing that object will then release the mutex. Code can then look something like:
using(acq = myMutex.Acquire())
{
... stuff that examines but doesn't modify the guarded resource
acq.EnterDanger();
... actions which might invalidate the guarded resource
... actions which make it valid again
acq.LeaveDanger();
... possibly more stuff that examines but doesn't modify the resource
}
If the inner code fails between EnterDanger and LeaveDanger, then the acquisition object should invalidate the mutex by calling Dispose on it, since the guarded resource may be in a corrupted state. If the inner code fails elsewhere, the mutex should be released since the guarded resource is in a valid state, and the code within the using block won't need to access it anymore. I don't have any particular recommendations of libraries implementing that pattern, but it isn't particularly difficult to implement as a wrapper around other kinds of mutex.

We need to understand more then .net to know what is going on the start of the MSDN page gives the first hint that someone “odd” is going on:
A synchronization primitive that can also be used for interprocess
synchronization.
A Mutex is a Win32 “Named Object”, each process locks it by name, the .net object is just a wrapper round the Win32 calls. The Muxtex itself lives within the Windows Kernal address space, not your application address space.
In most cases you are better off using a Monitor, if you are only trying to synchronizes access to objects within a single process.

If you need to garantee that the mutex is released switch to a try catch finally block and put the mutex release in the finally block. It is assumed that you own and have a handle for the mutex. That logic needs to be included before release is invoked.

Reading the documentation for ReleaseMutex, it seems the design decision was that a Mutex should be released consciously. if ReleaseMutex isn't called, it signifies an abnormal exit of the protected section. putting the release in a finally or dispose, circumvents this mechanism. you are still free to ignore the AbandonedMutexException, of course.

Be aware: The Mutex.Dispose() executed by the Garbage collector fails because the garbage collection process does not own the handle according Windows.

Dispose depends on WaitHandle to be released. So, even though using calls Dispose, it won't go into affect until the the conditions of stable state are met. When you call ReleaseMutex you're telling the system that you're releasing the resource, and thus, it is free to dispose of it.

For the last question.
Is there any benefit to putting Mutex in a using block? Or, should I just new up a Mutex instance, wrap it in a try/catch, and call ReleaseMutex() within the finally block (Basically implementing exactly what I thought Dispose() would do)
If you don't dispose of the mutex object, creating too many mutex objects may encounter the following issue.
---> (Inner Exception #4) System.IO.IOException: Not enough storage is available to process this command. : 'ABCDEFGHIJK'
at System.Threading.Mutex.CreateMutexCore(Boolean initiallyOwned, String name, Boolean& createdNew)
at NormalizationService.Controllers.PhysicalChunkingController.Store(Chunk chunk, Stream bytes) in /usr/local/...
The program uses the named mutex and runs 200,000 times in the parallel for loop. Adding using statement resolves the issue.

Related

Unity C# - Is infinite loop thread terminated when class is destroyed?

So I have a game object that contains a class A (extends monobehavior) that contains(creates) a class B (no monobehavior). This class B spawns a thread that contains a while (true) loop which does some work.
My question is...
When this GameObject is disposed/destroyed , I assume the class B contained inside of it is destructed as well. When this class B is destructed, is this infinite loop thread terminated as well?
Threads that have been started, principally run until the main thread ends. This is a OS level fallback, to avoid Zombie Threads without a owning application. However, it is not a fallback you should rely on. A Thread once started should be seen as a unmanaged resource, so cleanup code should be in place. The Dispose pattern in particular.
There is this wonderfull article on how to do it, but I feel it needs a explanation. You generally have to differentiate two cases with the Dispose pattern:
You are working with a Unamanged resource directly. In that case you write the Finalizer first. Then provide Dispose as a convenient way to clean stuff up on a programmers decision.
You are handling or may be handling something that implements IDisposeable. In that case, all you implement is the Dispose part and for the sole purpose of cascading teh call down to the Disposeable stuff you wrap around.
95% of all classes only implement IDisposeable because of case 2. We do not know if there actually is anything Disposeable, or if that was just part of a abstract base class.
Thread does not implement Dispose (I have no idea why), so you got a rare case 1. Dispose and Finalizer are often also compressed into one method - usually the Dispose one - as their code is very similar. The only real difference between Finalize and Dispose is cascading behavior:
When Finalizing, you never cascade. Finalisation is between this instance and the GC.
When Disposing, you always cascade (when possible). Most of the cases it is only there to allow cascading.
My final advice is, that since you are working with Multithreading you have to take care not to swallow Exceptions. Normally you got to write really bad code for to swallow Exceptions, but with Multitasking you have to go out of your way to not swallow any. I can not find any indication that Thread has a build in mechanic to persist exceptions (I know for a fact that Task does). All I can give you is the two articles on exception handling I link often:
https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/
https://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET

Is it necessary to cancel Task inside of finalizer?

I have viewModelA and viewA. I run some time consuming operation in constructor of viewModelA:
public class ViewModelA
{
Task task;
CancellationTokenSource token;
public viewModelA()
{
task = new Task(TimeConsumingOperation, token.Token);
}
private void TimeConsumingMethod()
{
//some time consuming operation
}
~ViewModelA()
{
token.Cancel();
}
}
Let's imagine that I run this application which consists just viewA and viewModelA and program starts some time-consuming operation(TimeConsumingMethod()) and suddenly I would like to immediately close the program, but I know that TimeConsumingMethod() is still running.
So my question is should I cancel a Task inside of finalizer? Or maybe I just should not create a finalizer method cause finalizer should be called for unmanaged resources?
Your proposal certainly seems like an abuse of the finalizer, at first glance. As noted, finalizers are typically for cleaning up unmanaged resources. More importantly, finalizers should not in fact be part of the contractual design of an object. They exist solely for the purpose of acting as a backstop for buggy code, i.e. to clean up resources when the client code has failed to do so explicitly (e.g. by calling IDisposable.Dispose()).
So the first thing I would look at is how non-buggy code should interact with your class. Is there in fact an IDisposable implementation? If not, then there also shouldn't be a finalizer. Do you feel strongly you need a finalizer? Then your class should implement IDisposable (or an equivalent), so that correct code can clean up the object efficiently.
Now, the second thing to look at is whether this task needs to be cancelled at all. What is it that you hope to accomplish by cancelling the task? Do you expect to need to cancel the task in a scenario other than exiting the process? How is the task itself implemented? Do you even start the task anywhere? These are all questions that are not addressed in your question, so there's no way for anyone to address them directly.
I will point out though that, assuming you call the Start() method at some point, the default implementation for the Task object is to execute the code using a thread pool thread. Thread pool threads are all background threads, and those threads are killed automatically when all of the foreground threads have exited, allowing the process itself to terminate normally.
So if all you're worried about is the state of the task when the process exits, and you are using the default implementation for the task, and the task can be safely interrupted at any time without corrupting data or leaving some temporary state active (e.g. a temporary file that should be deleted when the task completes), then I don't think you need to cancel the task explicitly.
On the other hand, if there is some reason to cancel the task explicitly, the right way to handle that is to provide a mechanism (e.g. implement IDisposable or something more explicit) that the client code can use to explicitly notify your object that it is no longer needed and should clean up. When the client code invokes this mechanism, then you can cancel the task.
In this case, you might want to implement a finalizer, but do so knowing that for correctly behaved client code, this finalizer will never be called. It's there only to protect against badly behaved code. It is also very important to understand that there's no guarantee at all that the finalizer will ever be called even for badly behaved code, and the most likely scenario for it to fail to be called is in fact when the process exits.
Finally, if you find you feel a finalizer is in fact required, I urge you to look at the SafeHandle class. This is a .NET class you can use as a base class for a helper object that will abstract the disposable nature of your task object. In that way, your own object need not implement a finalizer itself; instead, the SafeHandle subclass you implement will automatically address that need.

Dispatcher does not dispose on exit

I am creating a wrapper for a COM library that interacts with IBM mainframes. It can only be accessed from a single thread. To get around this, I've created a System.Windows.Threading.Dispatcher to handle running all interactions on a dedicated thread.
My problem is that if the object is not disposed explicitly, the dispatcher stays running after a WinForm application exits. The finalize method is never called for the object that creates the dispatcher. I need the sessions to be closed reliably to prevent unnecessary connections.
If I call GC.Collect on application exit, it will close fine. However, the library that I created will be used by mostly inexperienced developers. I cannot count on them always Disposing, collecting garbage or all committing to either WinForms or WPF to hook into application exit events.
I've read that if a class has a finalizer, its cleanup gets deferred until later. That may be part of the issue, but I can get around having a finalizer?
The finalize method is never called for the object that creates the dispatcher
The finalizer is called, when GC decides to perform grabage collection. You shouldn't rely on finalizer, when you need to dispose resources explicitly, because you shouldn't interfere in GC work.
I cannot count on them always Disposing
I'm afraid, you have no choice. Implement IDisposable and force your users to call Dispose. This is normal practice in .NET.
Using WPF's dispatcher in a Winforms app isn't exactly a great idea. Check this answer for the equivalent Winforms approach.
Getting the COM objects released otherwise doesn't take a great effort. Just set the thread's IsBackground property to true. Which will make the CLR automatically abort the thread when the program's main thread exits. The CLR then runs one final garbage collection, the exact equivalent of you calling GC.Collect() explicitly.

Which of the below Mutex expressions ideally prevents multiple instances of .Net application and what is the difference?

Typically I see these two pieces of code all around. Both works in my case too, but which should I stick to?
Case 1:
bool isNew = false;
Mutex mutex = new Mutex(true, "MyApp_Mutex", out isNew);
if (!isNew)
{
MessageBox.Show("already running.", "Multiple Instances Not Allowed",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
return;
}
Case 2:
Mutex mutex = new Mutex(false, "MyApp_Mutex"))
if (!mutex.WaitOne(0, false))
{
MessageBox.Show("already running.", "Multiple Instances Not Allowed",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
return;
}
Which is the ideal way between the two to prevent multiple instances?
What is the difference?
Moreover I see codes like these:
//if not return{
mutex.ReleaseMutex();
GC.Collect();
//application.Run();
GC.KeepAlive(mutex);
under the second method but never with the first. Why is that so? Or did I get that wrong?
Basically it lies with the proper understanding of the parameters and methods used. I would appreciate if someone can briefly detail it, I understand not half when reading msdn documentation..
In the first case, you're asking the OS to create the mutex and give you ownership of it if it's created - this is done through the first parameter, initiallyOwned. The isNew parameter tells you whether or not it was a new mutex. If it's new then you're guaranteed to have ownership of it since that's what you asked for with the initiallyOwned parameter. Since it's new, and you own it, you know there are no other instances of the application running, because if there were, they would have already created the mutex and they would own it.
The second case is basically the same thing, but done in a slightly different way. It's not requesting ownership on the create, but on the WaitOne call. WaitOne is requesting ownership, and waiting for 0 milliseconds. If you get ownership then you know no other instance of your app is running, for the same reasons as case 1.
As for which to use, to my knowledge it doesn't matter. The first seems to be more intuitive, at least to me.
Adding answer to new question #3
When the app is complete it should release the mutex since it owns it. .NET will likely release it for you when your app exits, but it's good practice to do it. GC.Collect and GC.KeepAlive are dealing with garbage collection. I can't think of any scenario why these calls would be needed when dealing with the mutexes controlling startup. I declare my mutex as static, so it will always be in scope and won't be freed by the garbage collector for the lifetime of my app.
In order to understand what is going on in these statements, one must understand what a mutex is and how they operate. I won't go into any great detail but I will point you to this reference book. Read the first few chapters until you get to the section about Mutexes.
The declaration of the Mutex in your first snippet of code uses the isNew bool to specify that the current application instance was the first to run, as well as the first instance to create the Mutex. This means each secondary execution of your application can be informed that their handle to the system-wide Mutex was not the first one to create and access the Mutex.
The following if block then checks to see if the associated task was the first to signal the Mutex and then handles the state accordingly.
The second snippet of code is completely different. A Mutex can be used in a task (task B) to wait for another task (task A) to signal the Mutex that any other task (task B) can continue.
The WaitOne(secs, releaseConext) method is saying, wait for the Mutex to send a Signal response for X seconds if it is locked by another thread. If the method doesn't get a signal response in X seconds, it returns false and, in the case of your sample code, enters the if block which is used to close the application.
Personally, I would use the first snippet of code. I would imagine that they both operate with the same amount of overhead. However, in choosing which version to use I would consider the first method to be the best one to use as a general matter of practice.

Disposing of COM in multi-threaded environment

I'm trying to properly dispose of a legacy VFP (FoxPro) COM control wrapped in a RCW generated by Visual Studio. The control exposes a Destroy method I should call to allow the control to properly tear itself down. There is a very good chance a method on the control may be executing on a background thread when a request is made to dispose of the COM instance. VFP is a single-threaded apartment model, so when calling Destroy it should just be added to the VFP execution stack.
Calling Destroy would ideally be the right thing to do as it allows the COM instance to clean up some resources. My concern is that instantiating a VFP COM control actually starts up a VFP language runtime instance that the control is hosted in and that instance may be locked up (non-responsive). This COM component exposes functionality in a large enterprise-scale 20-year-old legacy app and I have seen situations where a .NET thread attempting to call a method on this control simply blocks without throwing an error (always caused by bugs in the legacy VFP code). This doesn't happen often, but it is often enough that it prompted me to build an instance manager that runs methods on the VFP COM instance in a background thread and periodically checks to see if that thread is blocked, and if so, destroys the COM instance and thread and restarts a new instance to monitor.
Is this the right way to dispose of the thread that a background method may be executing on?
Should I attempt to get fancier by trying to call the Destroy method to allow the COM control to properly tear down?
if (_vfpThread != null)
{
try
{
if (_vfpThread.IsAlive)
_vfpThread.Abort();
}
catch (ThreadAbortException)
{ }
finally
{
_vfpThread = null;
}
}
if (_vfpInstance != null)
{
Marshal.ReleaseComObject(_vfpInstance);
_vfpInstance = null;
}
When a method call is pending on a VFP-based COM object (which always runs in an STA apartment), invoking any method on that same COM object from another thread will block until the former call returns (exits the apartment).
That means, any thread attempting to call Destroy() concurrently will be at the mercy of that first thread. And if that thread doesn't know to exit voluntarily, it could in theory keep the disposing thread blocked indefinitely. So, in other words, there's no direct way to ask the 1st thread to exit the method immediately by calling another method on the COM object from within another thread. Calling _vfpThread.Abort() should work, but the safety of this approach largely depends on the internals of the VFP class.
In many cases, due to it being legacy code, it won't have anything like a try/catch/finally section that would allow for a graceful exit, therefore resources may wind up being left unreleased. - BAD!
Another approach would be to set an external flag somewhere (registry, file, whatever), which would be available for reading by that 1st thread from within the method it is executing. That of course requires that the VFP class be aware of having to read the flag from each of its COM-published methods, and act accordingly and quickly.
Also, regarding your code snippet.
Catching ThreadAbortException in the code that is aborting a thread only makes sense if the thread executing this code is aborting itself. Which would be pretty awkward, since it could instead just return from the method. (Or, is this thread that is calling _vfpThread.Abort() also potentially being aborted from yet another thread?)
In a normal scenario, what you'd need wrapped in a ThreadAbortException catcher is the 1st thread's main code that performs calls to all those business methods on the COM object.
Ideally, you'd have this as deep down the stack as the VFP methods themselves where the code would be able to gracefully close all resources/tables etc before re-throwing the exception.
And then in the main method that you passed to the ThreadStart, you'd have a similar catcher except it would peacefully return from the method, thereby terminating the thread or releasing it to the thread pool.
Yes, I did understand your code correctly.-Thanks.
Aborting the vfp thread if it does not exit gracefully within 60 seconds is perhaps the only thing you could do.
In terms of what Dispose should do - it should try its best to release all unmanaged resources, which are unfortunately hidden from this code as they are used/opened from within the VFP COM class. So, if the COM object is seized, the main thread won't be able to force it to release those resources. Perhaps what you could try doing is wrapping the entire body of the COM business method in a VFP try-catch block and releasing resources/closing tables in the catch section. There's a good chance that the try-catch block would capture the ThreadAbortException caused by calling _vfpThread.Abort() from the main thread.

Categories