Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: Channel sessions not cleaning up (Avoiding Memory Leaks)

$
0
0
The code in SshCommand 2013.4.7 (EndExecute):
        public string EndExecute(IAsyncResult asyncResult)
        {
            if (this._asyncResult == asyncResult && this._asyncResult != null)
            {
                lock (this._endExecuteLock)
                {
                    if (this._asyncResult != null)
                    {
                        //  Make sure that operation completed if not wait for it to finish
                        this.WaitHandle(this._asyncResult.AsyncWaitHandle);

                        if (this._channel.IsOpen)
                        {
                            this._channel.SendEof();

                            this._channel.Close();
                        }

                        this._channel = null;
                        this._asyncResult = null;

                        return this.Result;
                    }
                }
            }

            throw new ArgumentException("Either the IAsyncResult object did not come from the corresponding async method on this type, or EndExecute was called multiple times with the same IAsyncResult.");
        }
If _channel is set to null here, it appears _channel will not be properly disposed (Because SshCommand's Dispose() checks for _channel being null).

Additionally _asyncResult.AsyncWaitHandle.Dispose() isn't being called. I don't know if that will be properly GC'd (The web discussions seem a bit conflicted on this topic).

I'm going to try the following code:
        public string EndExecute(IAsyncResult asyncResult)
        {
            if (this._asyncResult == asyncResult && this._asyncResult != null)
            {
                lock (this._endExecuteLock)
                {
                    if (this._asyncResult != null)
                    {
                        //  Make sure that operation completed if not wait for it to finish
                        this.WaitHandle(this._asyncResult.AsyncWaitHandle);

                        if (this._channel.IsOpen)
                        {
                            this._channel.SendEof();

                            this._channel.Close();
                        }

                        // Dispose managed ResourceMessages.
                        if (this._channel != null)
                        {
                            this._channel.DataReceived -= Channel_DataReceived;
                            this._channel.ExtendedDataReceived -= Channel_ExtendedDataReceived;
                            this._channel.RequestReceived -= Channel_RequestReceived;
                            this._channel.Closed -= Channel_Closed;

                            this._channel.Dispose();
                            this._channel = null;
                        }

                        this._asyncResult.AsyncWaitHandle.Dispose();
                        this._asyncResult = null;

                        return this.Result;
                    }
                }
            }

            throw new ArgumentException("Either the IAsyncResult object did not come from the corresponding async method on this type, or EndExecute was called multiple times with the same IAsyncResult.");
        }
I'll write back with my results.

Viewing all articles
Browse latest Browse all 1729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>