From ef584b16f69830d42b65bdf67befcb0b5cbef8e3 Mon Sep 17 00:00:00 2001 From: olegkap_cp Date: Thu, 20 Dec 2012 19:33:19 +0000 Subject: [PATCH] Fix cleanup of SftpFileStream. --- .../Renci.SshNet/Sftp/SftpFileStream.cs | 43 ++++++++++++++----- Renci.SshClient/Renci.SshNet/ShellStream.cs | 6 +++ .../Renci.SshNet/SubsystemSession.cs | 10 +++++ 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/Renci.SshClient/Renci.SshNet/Sftp/SftpFileStream.cs b/Renci.SshClient/Renci.SshNet/Sftp/SftpFileStream.cs index 8be944ef..66d028f9 100644 --- a/Renci.SshClient/Renci.SshNet/Sftp/SftpFileStream.cs +++ b/Renci.SshClient/Renci.SshNet/Sftp/SftpFileStream.cs @@ -5,6 +5,7 @@ using System.Text; using System.IO; using System.Threading; using System.Diagnostics.CodeAnalysis; +using Renci.SshNet.Common; namespace Renci.SshNet.Sftp { @@ -240,6 +241,7 @@ namespace Renci.SshNet.Sftp this._bufferOwnedByWrite = false; this._canSeek = true; this._position = 0; + this._session.Disconnected += Session_Disconnected; var flags = Flags.None; @@ -487,9 +489,9 @@ namespace Renci.SshNet.Sftp this._bufferPosn = 0; var data = this._session.RequestRead(this._handle, (ulong)this.Position, (uint)this._bufferSize); - + this._bufferLen = data.Length; - + Buffer.BlockCopy(data, 0, this._buffer, 0, this._bufferLen); if (this._bufferLen < 0) @@ -902,20 +904,30 @@ namespace Renci.SshNet.Sftp { base.Dispose(disposing); - lock (this._lock) + if (this._session != null) { - if (this._handle != null) + lock (this._lock) { - if (this._bufferOwnedByWrite) + if (this._session != null) { - this.FlushWriteBuffer(); - } + if (this._handle != null) + { + if (this._bufferOwnedByWrite) + { + this.FlushWriteBuffer(); + } - if (this._ownsHandle) - { - this._session.RequestClose(this._handle); + if (this._ownsHandle) + { + this._session.RequestClose(this._handle); + } + + this._handle = null; + } + + this._session.Disconnected -= Session_Disconnected; + this._session = null; } - this._handle = null; } } } @@ -995,5 +1007,14 @@ namespace Renci.SshNet.Sftp this._bufferOwnedByWrite = true; } } + + private void Session_Disconnected(object sender, EventArgs e) + { + lock (this._lock) + { + this._session.Disconnected -= Session_Disconnected; + this._session = null; + } + } } } \ No newline at end of file diff --git a/Renci.SshClient/Renci.SshNet/ShellStream.cs b/Renci.SshClient/Renci.SshNet/ShellStream.cs index cba6e2ab..180b623b 100644 --- a/Renci.SshClient/Renci.SshNet/ShellStream.cs +++ b/Renci.SshClient/Renci.SshNet/ShellStream.cs @@ -480,6 +480,12 @@ namespace Renci.SshNet this._channel.Dispose(); this._channel = null; } + + if (this._session != null) + { + this._session.Disconnected -= new EventHandler(Session_Disconnected); + this._session.ErrorOccured -= new EventHandler(Session_ErrorOccured); + } } private void Session_ErrorOccured(object sender, ExceptionEventArgs e) diff --git a/Renci.SshClient/Renci.SshNet/SubsystemSession.cs b/Renci.SshClient/Renci.SshNet/SubsystemSession.cs index 3f13dbb9..f0ffa19b 100644 --- a/Renci.SshClient/Renci.SshNet/SubsystemSession.cs +++ b/Renci.SshClient/Renci.SshNet/SubsystemSession.cs @@ -39,6 +39,11 @@ namespace Renci.SshNet.Sftp /// public event EventHandler ErrorOccurred; + /// + /// Occurs when session has been disconnected form the server. + /// + public event EventHandler Disconnected; + /// /// Gets the channel number. /// @@ -161,6 +166,11 @@ namespace Renci.SshNet.Sftp private void Session_Disconnected(object sender, EventArgs e) { + if (this.Disconnected != null) + { + this.Disconnected(this, new EventArgs()); + } + this.RaiseError(new SshException("Connection was lost")); }