Problem disposing of socket

0 votes

I'm working with some code (not mine I hasten to add, I don't trust this much at all) for a class which opens a socket, makes requests and listens for responses, which is throwing an exception in a way I can't comprehend when tested in x-unit. I assume the same exception happens "live" but the class is referenced by a singleton so it is probably just hidden.

The problem manifests as "System.CannotUnloadAppDomainException: Error while unloading app domain" in x-unit and the inner exception is "System.ObjectDisposedException" thrown (essentially) inside the finalizer when closing the socket! There is no other reference to the socket which calls close and disposes of is protected on the Socket class so I'm not clear how else the object could be disposed of.

Further, if I merely catch and absorb the ObjectDisposedException xunit terminates when it hits the line to close the listener thread.

I just don't get how the Socket can be disposed before it's asked to close.

My knowledge of sockets is only what I've learnt since finding this problem, so I don't know if I've provided everything SO might need. LMK if not!

public class Foo
{
    private Socket sock = null;
    private Thread tListenerThread = null
    private bool bInitialised;
    private Object InitLock = null;
    private Object DeInitLock = null;

    public Foo()
    {
        bInitialised = false;

        InitLock = new Object();
        DeInitLock = new Object();
    }

    public bool initialise()
    {
        if (null == InitLock)
            return false;

        lock (InitLock)
        {
            if (bInitialised)
                return false;

            sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 8);
            sock.Bind( /*localIpEndPoint*/);
            sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(mcIP));

            tListenerThread = new Thread(new ThreadStart(listener));
            tListenerThread.Start();

            bInitialised = true;
            return true;
        }
    }

    ~Foo()
    {
        if (bInitialised)
            deInitialise();
    }

    private void deInitialise()
    {
        if (null == DeInitLock)
            return;

        lock (DeInitLock)
        {
            if (bInitialised)
            {
                sock.Shutdown(SocketShutdown.Both); //throws System.ObjectDisposedException
                sock.Close();

                tListenerThread.Abort(); //terminates xunit test!
                tListenerThread = null;

                sock = null;

                bInitialised = false;
            }
        }
    }
}
Oct 15, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,175 views

1 answer to this question.

0 votes

If this object is eligible for garbage collection and there are no other references to the Socket, then the socket's finalizer may well run before your object's finalizer. I suspect that's what's happened here.

It's generally a bad idea (IMO) to do this much work in a finalizer. I can't remember the last time I implemented a finalizer at all - if you implement IDisposable, you should be fine unless you have direct references to unmanaged resources, which are almost always in the form of IntPtrs. The orderly shutdown should be the norm - a finalizer should only usually run if either the program is shutting down, or someone has forgotten to dispose of the instance to start with.

(I know you clarified at the start that this isn't your code - I just thought I'd explain why it's problematic. Apologies if you already knew some/all of this.)

answered Oct 15, 2018 by Annie97
• 2,160 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

Downloading source code of Android Things

No because AndroidThings is still in preview ...READ MORE

answered Jul 5, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,885 views
0 votes
1 answer

What is the size of CoAP packet?

It Depends: Core CoAP messages must be small enough ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
2,587 views
0 votes
1 answer

Problem while disconnecting BLE device!

I think that's because of the way ...READ MORE

answered Jul 18, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
3,715 views
0 votes
1 answer

Android AllJoyn: Connection with second machine gives error of BusAttachement

Why dont you create two Interfaces, one ...READ MORE

answered Jul 25, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
717 views
0 votes
1 answer

Configuring a Windows 10 IoT App for Automatic Restart

Have you tried using powershell right after ...READ MORE

answered Oct 10, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,003 views
0 votes
1 answer

Access Serial Ports in Windows 10 IoT App

On Windows IoT you have to use ...READ MORE

answered Oct 10, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
2,169 views
0 votes
1 answer

Testing your code for speed?

What you are describing is known as ...READ MORE

answered Nov 13, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
598 views
0 votes
1 answer

Identification of vulnerable code in an IoT node

The software running on a device is ...READ MORE

answered Aug 30, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
744 views
0 votes
1 answer

MQTT : Connection lost on MQTT subscriber to Internet of Things Server

An invalid topic "matteo" seems to be causing the ...READ MORE

answered Oct 3, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
4,643 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP